Reputation: 575
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 :)
Upvotes: 5
Views: 7071
Reputation: 9035
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.
Upvotes: 6
Reputation: 7663
The deafault checked item is set by the middle argument in setSingleChoiceItems
alertDialogBuilder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
In your code you have that set to -1 which means no item will be selected by default. Just change the value to the one in your array that you want selected. Remember to start with 0 for the first one and count your way up to the item you want selected.
Upvotes: 0
Reputation: 984
Please check the following android.app.AlertDialog.Builder.setSingleChoiceItems(CharSequence[] items, int
checkedItem, OnClickListener listener)
Give the integer value of the position of items as second parameter checkedItem.
To make default value as 15th item give the following
alertDialogBuilder.setSingleChoiceItems(items, 14, new DialogInterface.OnClickListener()
Upvotes: 0