Android AlertDialog with List

0

In this article, we will see an alert dialog with a single-choice list which means there won’t be any buttons and on touching an item, the dialog will disappear.

Main Screen

The main screen contains a button, clicking which will open the alert dialog. We have set the onClick attribute to the method-to-be-invoked to show the dialog.

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/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/open_dialog"
        android:layout_gravity="center_horizontal"
        android:onClick="openDialog" />

</LinearLayout>

List of values

We will specify the list of values to be shown in the strings.xml. It will contain a list of values contain programming languages.

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="open_dialog">Open Languages</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>

Dialog Fragment

Class AlertDialogWithListExample will be responsible to create the Dialog. We use AlertDialog.Builder to build the dialog. The class must extend DialogFragment so that the lifecycle of the Dialog is automatically managed. Read my example on DialogFragment to know more about it.
We also call setItems to set the list of languages. Along with the list of items resource, we pass OnClickListener object which simply shows the language selected.

AlertDialogWithListExample:

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 AlertDialogWithListExample extends DialogFragment {
	@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)
        .setItems(R.array.languages, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getActivity(), languages[which], Toast.LENGTH_SHORT).show();
			}
		});
		AlertDialog dialog = alertDialogBuilder.create();
		return dialog;
	}
}

Run the Application

Main screen.

Click button to open Dialog

Click button to open Dialog

Click on ‘Open Languages’ to open the dialog box with the list.

List of Languages

List of Languages

The dialog vanishes as soon as an item is touched.

Select Scala

Select Scala

Download source code

This was an example about Dialog with single-choice list items. You can download the source code here: alertDialogWithList.zip

Share.

Comments are closed.