Alert Dialog with Radio Button Option in Android

by Sasikumar 2014-03-12 12:27:06

Alert Dialog box with radio button option in android:
This helps in offering options to the user for choosing a single option from multiple option in a alert dialog, for example choosing difficulty level in a game. We can use the following bit of code,
AlertDialog diffLevelDialog;
final CharSequence[] options = {" Extreme Hard "," Hard "," Medium "," Easy "}; // Options to be shown in Alert Dialog
// Create and Build the Dialog
AlertDialog.Builder mybuilder = new AlertDialog.Builder(this);
mybuilder.setTitle("Choose Level of Difficulty");
mybuilder.setSingleChoiceItems(options, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int selectedOption) {
switch(selectedOption)
{
case 0:
// Process to be done when 1st option selected
break;
case 1:
// Process to be done when 2nd option selected
break;
case 2:
// Process to be done when 3rd option selected
break;
case 3:
// Process to be done when 4th option selected
break;
}
diffLevelDialog.dismiss();
}
});
diffLevelDialog = mybuilder.create();
diffLevelDialog.show();

Output :
Untitled.jpg
915
like
0
dislike
0
mail
flag

You must LOGIN to add comments