Displaying the Alert Dialog Box in Android

Here we will learn that how to display the alert dialog box on  button click.

Alert dialog box is used to display the  message/condition with two buttons cancel and OK.

Example:

Suppose when  you logout the Facebook then show dialog with message,Are you sure to logout ?, then this Alert Dialog.

# Implementation of Alert Dialog Box in Android

1.First we will create “main_activity.xml” layout where we will show the button

2.After that we will create “MainActivity.java” class where we will put the    logic of alert dialog box on the button click Listener;

Let’s start

1.First we will create “main_activity.xml” layout where we will show the button

main_activity.xml

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”/>
<Button
android id:”@+id/show_alert_dialog”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”/>
</LinearLayout>

2.After that we will create “MainActivity.java” class where we will put the    logic of button click.

MainActivity.java

package com.example.placementpreparation;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity
{
private Button show_alert_button;
@Override
public void onCreate(Bundle SavedInstaceState)
{
super.onCreate(SavedInstaceState);
setContentView(R.layout.main_activity);
show_alert_button=(Button)findViewById(R.id.show_alert_dialog);
show_alert_button.setOnClickListener(alertDialogListener);
}
 View.OnClickListener alertDialogListener= new View.OnClickListener() {
        @Override
        public void onClick(View v) {
               alertDialog();
   
        }
    };
 private void alertDialog() {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
          alertDialogBuilder .setCancelable(true);
        alertDialogBuilder.setIcon(R.drawable.image);
        alertDialogBuilder.setTitle("welcome to placementpreparation");
        alertDialogBuilder.setMessage("Do you want logout this application ?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss;

                    }
                });
 .setNegativeeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss;

                    }
                });
 AlertDialog alertDialog = alertDialogBuilder.create();

        alertDialog.show();
    }
}

Output:

Dialog will show with the:
1.Icon/image  
2.welcome to placementpreparation
4.Do you want logout this application ?
5.No      Yes

Explanation:

Here AlertDialog.Builder class provide the facilitate to set the icon/image,title and content/message for the alert dialog box.

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

2 thoughts on “Displaying the Alert Dialog Box in Android

Leave a Reply

Your email address will not be published. Required fields are marked *