articles

Home / DeveloperSection / Articles / Alert Box in Android

Alert Box in Android

Nitish Kesarwani 2605 22-Dec-2017
Alert Box Description

This article consists of information about Alert box formation in Android. I am going to tell you how to create an Alert box pop-up window in Android and their necessary component needs.

In this program, a single button is pushed against mouse to get an alert message option that provides optional window yes or no. If you click on the popup window then yes response to the click event. An alert box is generally used to receive a feedback from the user. This feedback is generally in terms of yes or no. The component of an alert dialog box is setPostiveButton and setNegativeButton value followed by onClickListener event. onClickListener invoke when the user clicks 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 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

                                      

                                    Alert Box in Android


Leave Comment

Comments

Liked By