How To Show Alert Dialog in Android

Create a new project in Android Studio

File – New Android Project

How To Show Alert Dialog in Android
How To Show Alert Dialog in Android

Dialogs

A dialog is a small window. if you wanted to ask the user about taking a decision between yes or no in response of any particular action taken by the user.

you need to make an object of AlertDialogBuilder which an inner class of AlertDialog.

Android alert dialog with three button

AlertDialog.Builder alertdialog = new AlertDialog.Builder(MainActivity.this);
                alertdialog.setTitle("Your Title");
                alertdialog.setMessage("You want to Delete").setCancelable(false)

                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "You Clicked on no", Toast.LENGTH_LONG).show();
                            }
                        }).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.finish();
                    }
                })
                        .setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "You Clicked on cancel", Toast.LENGTH_LONG).show();
                            }
                        });
                               alertdialog.show();

            }
        });

Android alert dialog with two button

AlertDialog.Builder alertdialog = new AlertDialog.Builder(MainActivity.this);
                alertdialog.setTitle("Your Title");
                alertdialog.setMessage("You want to Delete").setCancelable(false)

                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "You Clicked on no", Toast.LENGTH_LONG).show();
                            }
                        }).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.finish();
                    }
                });
                    alertdialog.show();

            }
        });

How To Show Alert Dialog in Android
How To Show Alert Dialog in Android

Related Posts

Leave a comment

Hey, so you decided to leave a comment! That's great. Just fill in the required fields and hit submit. Note that your comment will need to be reviewed before its published.