---
title: "Alert Box in Android"  
description: "The component of an alert dialog box is setPostiveButton and setNegativeButton value followed by onClickListener event. onClickListener invoke when th"  
author: "Nitish Kesarwani"  
published: 2017-12-22  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12737/alert-box-in-android  
category: "android apps"  
tags: ["android", "android activity", "android layout", "android alertdialog"]  
reading_time: 4 minutes  

---

# Alert Box in Android

##### Alert Box Description

This article consists of information about [Alert box](https://www.mindstick.com/articles/12940/how-to-create-message-alert-box-using-jquery) formation in Android. I am going to tell you how to create an Alert box pop-up window in Android and their necessary [component](https://www.mindstick.com/articles/12262/learn-how-to-make-a-component-in-magento-2) needs.

In this program, a single button is pushed against mouse to get an alert message option that provides [optional](https://www.mindstick.com/interview/22851/main-difference-between-optional-and-required-keywords) window yes or no. If you click on the popup window then yes [response](https://yourviews.mindstick.com/view/86896/biden-s-assertive-response-to-special-counsel-critique-on-memory-strength) to the [click event](https://www.mindstick.com/forum/424/how-to-call-form_paint-event-on-button-click-event). An alert box is generally used to receive a [feedback](https://www.mindstick.com/articles/12731/the-importance-of-feedback-to-the-user-experience) from the user. This feedback is generally in terms of yes or no. The component of an alert [dialog box](https://www.mindstick.com/blog/157/dialog-box-in-dot-net) is setPostiveButton and setNegativeButton value followed by [onClickListener](https://www.mindstick.com/forum/33726/inside-onclicklistener-i-cannot-access-a-lot-of-things-how-to-approach) event. onClickListener invoke when the [user clicks](https://answers.mindstick.com/qa/34556/if-a-user-clicks-all-the-google-ads-on-my-site-is-it-foul) on the button and response is generated. setPositiveButton is used to take a yes response from the user while setNegativeButton is used to provide no response on click the No text and the window is terminated. One of the most important [components](https://www.mindstick.com/blog/74096/understanding-the-role-of-some-integral-ac-components) is AlertDialog.Builder object which is used to create an alert dialog box.

```
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.alert">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 
```

\

###### MainActivity.java

```
package com.mapsample.msclient009.alert;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void open(View view)
    {  Button btn =(Button)findViewById(R.id.alerBox);
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setMessage("You Want to quit");
        alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "This yes Button", Toast.LENGTH_SHORT).show();
            }
        });
 alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialogInterface, int i) {
         Toast.makeText(MainActivity.this, "No Means NO....", Toast.LENGTH_SHORT).show();
         finish();
     }
 });
 AlertDialog alertDialog =alert.create();
  alertDialog.show();
    }
} 
```

###### DialogForm.java

```
package com.mapsample.msclient009.alert;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;

/**
 * Created by msclient009 on 12-12-2017.
 */

public class DialogFram extends DialogFragment {
    public Dialog createDialogFragment(Bundle savedInstanceState)
    {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText ( getContext (),"Pop up Screen",Toast.LENGTH_LONG ).show ();

            }
        }).setPositiveButton(R.string.Reject, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });

     return (builder.create());
    }
} 
```

\

```
Main_Activity.xml<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.mapsample.msclient009.alert.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Alert"
            android:onClick="open"
            android:id="@+id/alerBox"
            />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>
```

###### Strings.xml

\

```
<resources>
    <string name="app_name">Alert</string>
    <string name="fire">emptyString</string>
    <string name="Reject">cancel</string>

</resources>
```

![Alert Box in Android](https://www.mindstick.com/mindstickarticle/0b3914f2-fb85-417b-8ea9-b151ee5acb32/images/a6a89a33-760c-4ba1-916d-293e02f5c24b.png)

![Alert Box in Android](https://www.mindstick.com/mindstickarticle/0b3914f2-fb85-417b-8ea9-b151ee5acb32/images/d3c81f85-30c4-455b-b1cd-7be32b059bb4.png)

---

Original Source: https://www.mindstick.com/articles/12737/alert-box-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
