DialogBox is used in many android applications to display dialogs. There are different types of dialogs that are displayed within android applications such as AlertDialog, warning dialogs, and others. In this article, we will take a look at How to Display a Yes/No Dialog Box in Android. A sample video is given below to get an idea about what we are going to do in this article.
Note: This Android article covered in both Java and Kotlin languages.
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.
Step 2: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:id = "@+id/container"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
<!--on below line we are creating
a text for heading of our app-->
< TextView
android:id = "@+id/idTVHeading"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_above = "@id/idBtnDisplayDialog"
android:layout_margin = "20dp"
android:gravity = "center"
android:padding = "4dp"
android:text = "Yes/No Dialog Box"
android:textAlignment = "center"
android:textColor = "@color/purple_200"
android:textSize = "20sp"
android:textStyle = "bold" />
<!--on below line we are creating a
button for displaying a dialog-->
< Button
android:id = "@+id/idBtnDisplayDialog"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_centerInParent = "true"
android:layout_margin = "20dp"
android:text = "Show Dialog"
android:textAllCaps = "false" />
</ RelativeLayout >
|
Step 3: Working with the MainActivity file
Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail.
package com.gtappdevelopers.kotlingfgproject
import android.content.DialogInterface
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// on below line we are creating
// variable for image view.
lateinit var dialogBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// on below line we are initializing our variable with id.
dialogBtn = findViewById(R.id.idBtnDisplayDialog)
// on below line we are adding click listener for our button.
dialogBtn.setOnClickListener {
// on below line we are creating a dialog click listener
// variable and initializing it.
val dialogClickListener =
DialogInterface.OnClickListener { dialog, which ->
when (which) {
// on below line we are setting a click listener
// for our positive button
DialogInterface.BUTTON_POSITIVE -> {
// on below line we are displaying a toast message.
Toast.makeText( this , "Yes clicked" , Toast.LENGTH_SHORT).show()
}
// on below line we are setting click listener
// for our negative button.
DialogInterface.BUTTON_NEGATIVE -> {
// on below line we are dismissing our dialog box.
dialog.dismiss()
}
}
}
// on below line we are creating a builder variable for our alert dialog
val builder: AlertDialog.Builder = AlertDialog.Builder( this )
// on below line we are setting message for our dialog box.
builder.setMessage( "Select yes to display toast message and no to dismiss the dialog ?" )
// on below line we are setting positive
// button and setting text to it.
.setPositiveButton( "Yes" , dialogClickListener)
// on below line we are setting negative button
// and setting text to it.
.setNegativeButton( "No" , dialogClickListener)
// on below line we are calling
// show to display our dialog.
.show()
}
}
} |
package com.gtappdevelopers.kotlingfgproject;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// on below line we are creating a variable for our button.
private Button dialogBtn;
private DialogInterface.OnClickListener dialogClickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing variables with ids.
dialogBtn = findViewById(R.id.idBtnDisplayDialog);
// on below line we are adding click listener for our dialog button
dialogBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// on below line we are creating a dialog click listener
// variable and initializing it.
dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
// on below line we are setting a click listener
// for our positive button
case DialogInterface.BUTTON_POSITIVE:
// on below line we are displaying a toast message.
Toast.makeText(MainActivity. this , "Yes clicked" , Toast.LENGTH_SHORT).show();
break ;
// on below line we are setting click listener
// for our negative button.
case DialogInterface.BUTTON_NEGATIVE:
// on below line we are dismissing our dialog box.
dialog.dismiss();
}
}
};
// on below line we are creating a builder variable for our alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
// on below line we are setting message for our dialog box.
builder.setMessage( "Select yes to display toast message and no to dismiss the dialog ?" )
// on below line we are setting positive button
// and setting text to it.
.setPositiveButton( "Yes" , dialogClickListener)
// on below line we are setting negative button
// and setting text to it.
.setNegativeButton( "No" , dialogClickListener)
// on below line we are calling
// show to display our dialog.
.show();
}
});
}
} |
Now run your application to see the output of it.
Output: