Saturday, October 22, 2016

android - AlertDialog single choice list

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#a0c1c2"
    >
    <Button
        android:id="@+id/btn_alert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Alert Dialog"
        android:layout_gravity="top|center_horizontal"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Arrays;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private Button mButton;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mButton = (Button) findViewById(R.id.btn_alert);

        // Set a click listener for the button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Initializing a new alert dialog
                final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

                // Set the alert dialog title
                builder.setTitle("Choose a flower.");

                // Initializing an array of flowers
                final String[] flowers = new String[]{
                        "Daffodil",
                        "Dahlia",
                        "Day Lily",
                        "Delphinium",
                        "Desert Rose"
                };

                /*
                    AlertDialog.Builder setSingleChoiceItems (CharSequence[] items,
                                        int checkedItem,
                                        DialogInterface.OnClickListener listener)

                        Set a list of items to be displayed in the dialog as the content, you will
                        be notified of the selected item via the supplied listener. The list will
                        have a check mark displayed to the right of the text for the checked item.
                        Clicking on an item in the list will not dismiss the dialog. Clicking
                        on a button will dismiss the dialog.

                        Parameters
                            items CharSequence: the items to be displayed.
                            checkedItem int: specifies which item is checked. If -1 no items are checked.
                            listener DialogInterface.OnClickListener: notified when an item on
                                        the list is clicked. The dialog will not be dismissed when
                                        an item is clicked. It will only be dismissed if clicked
                                        on a button, if no buttons are supplied it's up to the
                                        user to dismiss the dialog.
                        Returns
                            AlertDialog.Builder This Builder object to allow for
                                                chaining of calls to set methods
                */

                // Set a single choice items list for alert dialog
                builder.setSingleChoiceItems(
                        flowers, // Items list
                        -1, // Index of checked item (-1 = no selection)
                        new DialogInterface.OnClickListener() // Item click listener
                        {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // Get the alert dialog selected item's text
                        String selectedItem = Arrays.asList(flowers).get(i);

                        // Display the selected item's text on snack bar
                        Snackbar.make(
                                mCLayout,
                                "Checked : " + selectedItem,
                                Snackbar.LENGTH_INDEFINITE
                        ).show();
                    }
                });

                // Set the a;ert dialog positive button
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // Just dismiss the alert dialog after selection
                        // Or do something now
                    }
                });

                // Create the alert dialog
                AlertDialog dialog = builder.create();

                // Finally, display the alert dialog
                dialog.show();
            }
        });
    }
}