How To Show Alert Dialog in Android - Mobile App Development, Android App Development, iphone App Development class="post-template-default single single-post postid-4137 single-format-standard wp-custom-logo"

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

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.