How to Add a Third Button to an Android Alert Dialog?

An Alert Dialog in Android is a small window that pops up on the screen to display important information, warnings, or confirmations to the user. It can be used to prompt the user for input, provide a message, or display an error.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Alert Dialog Boxes" android:textStyle="bold" android:textSize="20sp"/> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button 1" android:layout_margin="20dp" android:backgroundTint="#4CAF50" android:onClick="BTN1"/> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button 2" android:layout_margin="20dp" android:backgroundTint="#4CAF50" android:onClick="BTN2"/> <Button android:id="@+id/btn3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button 3" android:layout_margin="20dp" android:backgroundTint="#4CAF50" android:onClick="BTN3"/> </LinearLayout> |
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.
Java
package com.anas.gfgalertdialog; import androidx.appcompat.app.AlertDialog;import androidx.appcompat.app.AppCompatActivity; import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast; public class MainActivity extends AppCompatActivity{ Button btn1,btn2,btn3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1=findViewById(R.id.btn1); btn2=findViewById(R.id.btn2); btn3=findViewById(R.id.btn3); } public void BTN1(View v){ AlertDialog alert1=new AlertDialog.Builder(MainActivity.this).create(); alert1.setTitle("Alert Dialog 1"); alert1.setMessage("Alert Dialog with 1 Button"); alert1.setButton(Dialog.BUTTON_NEGATIVE,"Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this,"Yes Clicked",Toast.LENGTH_SHORT).show(); } }); alert1.show(); } public void BTN2(View v){ AlertDialog.Builder alert2=new AlertDialog.Builder(MainActivity.this); alert2.setTitle("Alert Dialog 2"); alert2.setMessage("Alert Dialog with 2 Buttons"); alert2.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this,"Yes Clicked",Toast.LENGTH_SHORT).show(); } }); alert2.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"No Clicked",Toast.LENGTH_SHORT).show(); } }); alert2.show(); } public void BTN3(View v){ AlertDialog.Builder alert3=new AlertDialog.Builder(MainActivity.this); alert3.setTitle("Alert Dialog 3"); alert3.setMessage("Alert Dialog with 3 buttons"); alert3.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this,"Yes Clicked",Toast.LENGTH_SHORT).show(); MainActivity.super.onBackPressed(); } }); alert3.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"No Clicked",Toast.LENGTH_SHORT).show(); } }); alert3.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"Cancel Clicked",Toast.LENGTH_SHORT).show(); } }); alert3.show(); }} |
Step 4:
Run the app.
Screenshot (Output):
activity_main.xml
Step 5:
Click on Button 1.
Screenshot (Output) :
Alert Dialog with 1 Button
Step 6:
Click on Button 2.
Screenshot (Output):
Alert Dialog with 2 Buttons
Step 7:
Click on Button 3.
Screenshot (Output):
Alert Dialog with 3 Buttons



