How to Create an Alert Dialog Box in Android?
Last Updated :
30 Aug, 2022
Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response, the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, and Action Button.
Alert Dialog code has three methods:
- setTitle() method for displaying the Alert Dialog box Title
- setMessage() method for displaying the message
- setIcon() method is used to set the icon on the Alert dialog box.
Then we add the two Buttons, setPositiveButton and setNegativeButton to our Alert Dialog Box as shown below.
Example:
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. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginTop = "180dp"
android:gravity = "center_horizontal"
android:text = "Press The Back Button of Your Phone."
android:textSize = "30dp"
android:textStyle = "bold" />
</ RelativeLayout >
|
Step 3: Working with the MainActivity File
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.
Java
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity. this );
builder.setMessage( "Do you want to exit ?" );
builder.setTitle( "Alert !" );
builder.setCancelable( false );
builder.setPositiveButton( "Yes" , (DialogInterface.OnClickListener) (dialog, which) -> {
finish();
});
builder.setNegativeButton( "No" , (DialogInterface.OnClickListener) (dialog, which) -> {
dialog.cancel();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
|
Kotlin
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onBackPressed() {
val builder = AlertDialog.Builder( this )
builder.setMessage( "Do you want to exit ?" )
builder.setTitle( "Alert !" )
builder.setCancelable( false )
builder.setPositiveButton( "Yes" ) {
dialog, which -> finish()
}
builder.setNegativeButton( "No" ) {
dialog, which -> dialog.cancel()
}
val alertDialog = builder.create()
alertDialog.show()
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...