Wednesday, April 1, 2020

Alert Dialog Box


Alert Dialog Box
Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.
Android AlertDialog is composed of three regions: title, content area and action buttons.
Android AlertDialog is the subclass of Dialog class.

e.g
  
    @Override  
protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
                  setContentView(R.layout.activity_main);  
   AlertDialog.Builder builder = new AlertDialog.Builder(this); //this will build a alert dialog 
        builder.setMessage("Do you want to close this application ?")   //this will show the msg on alert dialog
            .setCancelable(false)    //we cancel the alertbox
            .setPositiveButton("Yes"new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int id) {  
                finish();                        //what will happen if we click on YES, define in this
                }  
            })  
            .setNegativeButton("No"new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int id) {  
                //  Action for 'NO' Button  
                dialog.cancel();             // what will happen if we click on NO, define in this
             }  
            });  
  
        //Creating dialog box  
        AlertDialog alert = builder.create();  
        //Setting the title manually  
        alert.setTitle("AlertDialogExample");  
        alert.show();  
}

   

1 comment:

Anonymous said...

Nice๐Ÿ‘๐Ÿ‘

Alert Dialog Box

Alert Dialog Box Android AlertDialog   can be used to display the dialog message with OK and Cancel buttons. It can be used to inter...