Thursday, 6 August 2020

Showing Alert Dialog In Android

In Android we can easily show an Dialog.
There are 1 componenets of a Dialog

1: Ttitle
2: Message
3: Buttons

public void showDialog(Activity activity, String title, CharSequence message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);

    if (title != null) builder.setTitle(title);

    builder.setMessage(message); 
// Adding buttons
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            MyActivity.this.finish();
       }
   });
   builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog dialog = builder.create();
dialog.show();
}

No comments:

Post a Comment