In my article on AlertDialog with List, I showed you an AlertDialog
with a traditional single-choice list.
On touching an item, the dialog will disappear. When you open the dialog again, you wouldn’t be able to know the user’s current choice.
If you want to persist the user’s choice, then you must either use the persistent single-choice list or multiple-choice, if you are interested in more than one item.
In this article, we will see an example for each case.
Main Screen
Let’s start with the main screen. It contains two buttons. One will open the single-choice list and the other will open multiple-choice list. We have assigned the attribute android:onClick
to the method that we want to be called as soon as the user presses the button.
welcome.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.javarticles.android.DatePickerExample" > <TextView android:id="@+id/welcome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:layout_marginTop="18dp" android:text="@string/welcome" android:textColor="@color/welcome_text_color" android:textSize="20sp" /> <Button android:id="@+id/single_choice_btn" style="?android:attr/buttonStyleSmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="openDialogSingleChoice" android:text="@string/single_choice" /> <Button android:id="@+id/multi_choice_btn" style="?android:attr/buttonStyleSmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="openDialogMultiChoice" android:text="@string/multi_choice" /> </LinearLayout>
The list of items
Add the list of static items in strings.xml
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">JavArticles</string> <string name="welcome">AlertDialog with List Example</string> <string name="alert_dialog_title">Languages</string> <string name="ok_btn">OK</string> <string name="single_choice">Single Choice</string> <string name="multi_choice">Multi Choice</string> <string name="cancel_btn">Cancel</string> <string-array name="languages"> <item>C</item> <item>C++</item> <item>Java</item> <item>Scala</item> <item>Groovy</item> <item>Python</item> </string-array> </resources>
DialogFragment Classes
We will be using a DialogFragment extended class to create the dialog.
In onCreateDialog
, we will call AlertDialog.Builder
to build the alert dialog. It follows the builder pattern, so you call a method and then keep building on the returned builder object.
If you want to persist the user’s choice, you should set the items using setSingleChoiceItems()
or setMultiChoiceItems
for multiple-choice. This will create a single-choice list along with radio buttons.
We have three events here. One when the user selects an item. The second when user presses OK button and the third one when user presses Cancel button. Each event has an OnClickListener
object assigned to it. When the user selects an item, we will assign the choice to a member variable. When user presses OK, we will persist the choice so next time when user opens the dialog, we will use the persisted choice as the default selection.
Here is the single-choice dialog fragment.
AlertDialogSingleChoiceListExample:
package com.javarticles.android; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.widget.Toast; public class AlertDialogSingleChoiceListExample extends DialogFragment { private int selectedItemIndex = 2; private int clickedItemIndex = selectedItemIndex; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final String[] languages = getResources().getStringArray( R.array.languages); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( getActivity()) .setTitle(R.string.alert_dialog_title) .setSingleChoiceItems(R.array.languages, selectedItemIndex, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { clickedItemIndex = which; Toast.makeText(getActivity(), languages[which], Toast.LENGTH_SHORT).show(); } }) .setPositiveButton(R.string.ok_btn, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { selectedItemIndex = clickedItemIndex; } }) .setNegativeButton(R.string.cancel_btn, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog dialog = alertDialogBuilder.create(); return dialog; } }
Next is the multiple-choice dialog fragment.
AlertDialogMultipleChoiceListExample:
package com.javarticles.android; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.widget.Toast; public class AlertDialogMultipleChoiceListExample extends DialogFragment { private boolean[] selectedItems = new boolean[] { false, true, false, true, true, false }; private boolean[] clickedItems = selectedItems; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final String[] languages = getResources().getStringArray( R.array.languages); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( getActivity()) .setTitle(R.string.alert_dialog_title) .setMultiChoiceItems(R.array.languages, selectedItems, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { clickedItems[which]= isChecked; Toast.makeText(getActivity(), languages[which], Toast.LENGTH_SHORT).show(); } }) .setPositiveButton(R.string.ok_btn, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { selectedItems = clickedItems; } }) .setNegativeButton(R.string.cancel_btn, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog dialog = alertDialogBuilder.create(); return dialog; } }
Main Activity
In the main activity’s onCreate
, we create the DialogFragment
objects for each case. In openDialogSingleChoice
, we show the single-choice dialog. In openDialogMultiChoice
, we show the multiple-choice dialog.
MainActivity:
package com.javarticles.android; import android.app.Activity; import android.app.DialogFragment; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { private DialogFragment singleChoiceDialog; private DialogFragment multiChoiceDialog; public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome); singleChoiceDialog = new AlertDialogSingleChoiceListExample(); multiChoiceDialog = new AlertDialogMultipleChoiceListExample(); } public void openDialogSingleChoice(View view) { singleChoiceDialog.show(getFragmentManager(), "languagesDialog"); } public void openDialogMultiChoice(View view) { multiChoiceDialog.show(getFragmentManager(), "languagesDialog"); } }
Run the application
Main screen. Let’s press ‘Single Choice’ to open the single-choice list.
Select an item and press OK.
Multiple-choice list
After selection, if user re-opens the dialog, the same choice will be shown.
Download the source code
This was an example of AlertDialog
with single and multiple-choice persistent list. You can download the source code here: alertDialogWithPersistentList.zip