Android AlertDialog with 3 buttons example


You might have made use of Toast to display messages that fades in stays for few seconds and fades away. If you want to display some message and let user select the response (Yes or No) then we can make use of Dialog called as AlertDialog.

Android AlertDialog Example
Android AlertDialog Example

AlertDialog is a subclass of Dialog from android.app package. You can create a dialog with one, two or even three buttons. It is also possible to display only text message using setMessage() method.

There are three functions for adding Buttons to Android Dialog,

setPositiveButton(int textId, DialogInterface.OnClickListener listener) :

This is Yes button, when clicked the code written in the OnClickListener onClick() method will be displayed.

setNegativeButton(int textId, DialogInterface.OnClickListener listener) :

This is just like the setPositiveButton method which acts a "NO" negative button, we will write the logic in OnClickListener anonymous class.

setNeutralButton(int textId, DialogInterface.OnClickListener listener) :

This is how we can set the 3rd button. It is a called the Neutral button.

Android AlertDialog Snippet
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);


        builder.setTitle("AlertDialog Example");


        builder.setMessage("This is an Example of Android AlertDialog with 3 Buttons!!");


        //Button One : Yes
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "Yes button Clicked!", Toast.LENGTH_LONG).show();
            }
        });


        //Button Two : No
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "No button Clicked!", Toast.LENGTH_LONG).show();
                dialog.cancel();
            }
        });


        //Button Three : Neutral
        builder.setNeutralButton("Can't Say!", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "Neutral button Clicked!", Toast.LENGTH_LONG).show();
                dialog.cancel();
            }
        });


        AlertDialog diag = builder.create();
        diag.show();
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency