Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single choice selection dialog in android

Tags:

android

dialog

I am making an android program. In my app, I am using a single choice selection AlertDialog whose items are added programmatically. What I want to do is:

  • set a background color to the selected item next time user opens the dialog,
  • show selected item in the middle of the dialog(this is a problem because there are about 20 items).

Here is what I have:XML:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp" >



    <Button
        android:id="@+id/selectDateButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Please, select date" />

</LinearLayout>

JAVA:

public class ExperimentListView extends Activity {

private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private static Calendar calendar = Calendar.getInstance();
private static Button selectDateButton;
private static String[] items;
private static int selectedDatePosition = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_ex);

    selectDateButton = (Button)findViewById(R.id.selectDateButton);
    selectDateButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            items = new String[20];
            for (int i = 0; i < 20; i++) {
                items[i] = DATE_FORMAT.format(calendar.getTime());
                calendar.add(Calendar.DATE, 1);
            }
            showListView();
        }
    });

}

private void showListView() {
    AlertDialog.Builder builder = new AlertDialog.Builder(ExperimentListView.this);
    builder.setTitle("Select date");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            selectedDatePosition = which;
            selectDateButton.setText(items[selectedDatePosition]);
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.getListView().setSelection(selectedDatePosition);
    alertDialog.show();
 }
}

I haven't been able to find a solution to this so far and I would be grateful if someone can help. Thanks in advance.

like image 432
boburShox Avatar asked Dec 04 '12 05:12

boburShox


1 Answers

Instead of using builder.setItem use builder.setSingleChoiceItems with selected item position passed as an argumet like

    builder.setSingleChoiceItems(strArray, selected_pos, new DialogInterface.OnClickListener ()
    {
         @Override
         public void onClick(DialogInterface dialog, int which) 
         {

         }
    });
like image 110
Deepika Avatar answered Oct 04 '22 02:10

Deepika