Tuesday, July 2, 2013

How to start an activity if the user clicks on OK button in AlertDialog - Android?

Make use of this code to start an activity if the user clicks on OK button in AlertDialog,

AlertDialog.Builder alertDialog = new AlertDialog.Builder(Someclass.this);
    builder.setMessage("Are you sure you want to quit?")    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int which) { 
        Intent myIntent = new Intent(((Dialog) dialog).getContext(), Someclass2.class);        startActivity(myIntent);    
        return;  
        }        
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which) {
             // Do Nothing
     }

    alertDialog.show();

No comments:

Post a Comment