Here, I am going to discuss about alert dialog box in android application using Android Studio.

An alert dialog box is a small window that prompts some information to the user to make a decision or to get some additional information. Alert dialog android is used to show the message with a positive(OK) and Negative(Cancel) buttons. It is just used to provide and ask the user about their choice to continue or discontinue.We can also customise Alert dialog in android .

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from templates to implement alert dialog box in android .

1. Create a button to your main layout file :

We create a button to your main layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dialog"/> </LinearLayout>

2. Implement Alert Dialog to your Main_Activty.java :

Now open your Main_Activity.java file and implement click Listener to your onCreate method and open your Alert Dialog box in android through the click listener.

Click Listener :

btn.setOnclickListener(new View.onClickListener(){ @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setTitle("Title"); dialog.setMessage("Write your message here."); dialog.setCancelable(true); // Specifying a listener allows you to take an action before dismissing the dialog. // The dialog is automatically dismissed when a dialog button is clicked. dialog.setPositiveButton( "Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Continue with some operation } }); // A null listener allows the button to dismiss the dialog and take no further action. dialog.setNegativeButton( "No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog dialog.cancel(); } }); AlertDialog alert = dialog.create(); alert.show(); } });

Main_Activity.java

package com.technxtcodelabs.myapplication; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button)findViewById(R.id.btn); btn.setOnclickListener(new View.onClickListener(){ @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setTitle("Title"); dialog.setMessage("Write your message here."); dialog.setCancelable(true); // Specifying a listener allows you to take an action before dismissing the dialog. // The dialog is automatically dismissed when a dialog button is clicked. dialog.setPositiveButton( "Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Continue with some operation } }); // A null listener allows the button to dismiss the dialog and take no further action. dialog.setNegativeButton( "No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog dialog.cancel(); } }); AlertDialog alert = dialog.create(); alert.show(); } }); } }

Now run the project/app to test android alert dialog box in android device or any Emulator .

Share this: WhatsApp

Facebook

Twitter

Reddit

LinkedIn

