Single choice selection dialog in android
Solution 1
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)
{
}
});
Solution 2
1)To set the background color you can use custom view for your Alert Dialog
2)to show the selected item in the middle of the dialog you need to swap its position in the array which you are passing to list for alert dialog.
3)And as you want background color respected to the item previously selected ,use shared preferences to store item and background,and regain the background and item selected when user open the alert dialog.
And as Deepika said use setSingleChoiceItems for single choice selection.
Comments
-
boburShox almost 2 years
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.