Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set check default radio button android with alert dialog?

any body help me... how to set check default radio button with alert dialog when is launched..?

this my code , ex: i want to set radio button when launched where items is "15"

public void showDialog()
{
    final CharSequence[] items = {"5", "10", "15","20"};

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    alertDialogBuilder.setTitle("Set limit article");

    alertDialogBuilder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(SettingAppDisplay.this, "You selected item No." + item + ": " + items[item], Toast.LENGTH_SHORT).show();

                if (items[item].equals("5")) {
                    //do what you want
                }
                else if (items[item].equals("10")) {
                    //do what you want                                  
                }
                else if (items[item].equals("15")) {
                    //do what you want
                }
                else if (items[item].equals("20")) {
                    //do what you want
                }

                dialog.dismiss();
            }   
    });
    alertDialogBuilder.show();

}

thanks your paticipation .. sorry with my english :)

like image 484
bukanamay Avatar asked May 30 '13 12:05

bukanamay


1 Answers

Change second argument (checkedItem) in setSingleChoiceItems from -1 to what ever position of radio button you wanted to be checked, here i changed it to '1' so first radio button will be checked.

 alertDialogBuilder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(SettingAppDisplay.this, "You selected item No." + item + ": " + items[item], Toast.LENGTH_SHORT).show();

                if (items[item].equals("5")) {
                    //do what you want
                }
                else if (items[item].equals("10")) {
                    //do what you want                                  
                }
                else if (items[item].equals("15")) {
                    //do what you want
                }
                else if (items[item].equals("20")) {
                    //do what you want
                }

                dialog.dismiss();
            }   
    });

See docs

setSingleChoiceItems (Cursor cursor, int checkedItem, String labelColumn, 
DialogInterface.OnClickListener listener)

Parameters

cursor the cursor to retrieve the items from.

checkedItem specifies which item is checked. If -1 no items are checked.

labelColumn The column name on the cursor containing the string to display in the label.

listener 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.

like image 102
Arun C Avatar answered Oct 05 '22 05:10

Arun C