Friday, September 30, 2011

Create Alert Dialog in Android

To create Alert Dialog just call the following code where you want to like on button click on back button press or where ever you wish


Like this is an example where I asked user to exit the application on back button pressed :

@Override
    public void onBackPressed() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to EXIT?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) 
                  {
                     moveTaskToBack(true) ;
                     System.exit(0);
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
     }


No comments:

Post a Comment