Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to dismiss AlertDialog with radio buttons in android sdk

Tags:

android

i have created a alertdialog with two radio buttons in it.when user select an option i need to dismiss the alertdialog but i am not been able to dismiss it.

 final CharSequence[] items = {"First Option", "Second Option"};

 AlertDialog.Builder builder = new AlertDialog.Builder(context);
 builder.setTitle("Choose an option"); 
 builder.setSingleChoiceItems(items, -1, new  DialogInterface.OnClickListener() { 
       public void  onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), items[item],  Toast.LENGTH_SHORT).show(); 
       } 
 });
 final AlertDialog alert = builder.create();
 alert.show();

Can someone help me how to do this.

Thanks

like image 429
Rishi Avatar asked Jun 21 '11 06:06

Rishi


1 Answers

Please try this..

final CharSequence[] items = {"First Option", "Second Option"};

 AlertDialog.Builder builder = new AlertDialog.Builder(context);
 builder.setTitle("Choose an option"); 
 builder.setSingleChoiceItems(items, -1, new  DialogInterface.OnClickListener() { 
   public void  onClick(DialogInterface dialog, int item) {
        dialog.dismiss();
        Toast.makeText(getApplicationContext(), items[item],  Toast.LENGTH_SHORT).show(); 
   } 
});
final AlertDialog alert = builder.create();
alert.show();
like image 159
Nikhil Avatar answered Mar 04 '23 02:03

Nikhil