Alert Box in Android

Introduction

Alert Boxes, Pop Ups and Notification Messages are an integral part of any app. Whatever the app platform it is, whether Windows Phone OS, Android or iOS, they have their own API to handle these. Frankly speaking, you need these features frequently in most of your apps.

So, in this article we will try an Alert Box.



Procedures

Step 1

Before you start, create an Android project successfully (Alt+Shifty+N).

Step 2

In this demonstration, we will not make any changes in the XML layout file. Since we are trying to show an Alert Box while the Activity renders.

So, for this we will put our code in the MainActivity.java file.

First, we will assign all the values of the AlertBox.Builder class.

So, we start by adding the following packages:

  1. import android.app.AlertDialog;  
  2. import android.app.AlertDialog.Builder; 

Next on onCreate state, we will add an AlertDialog.Builder instance and assign their values.

  1.    // Instance of Alert Dialog  
  2.    AlertDialog.Builder builder=new Builder(MainActivity.this);  
  3.    builder.setMessage("WOW! I have created a DialogBox :)")  
  4.    .setCancelable(false)  
  5.    .setPositiveButton("Yes"new DialogInterface.OnClickListener() {  
  6.    @Override  
  7.    public void onClick(DialogInterface arg0, int arg1) {  
  8.       finish(); // Close The Activity  
  9.    }  
  10. })  
  11.    .setNegativeButton("No"new DialogInterface.OnClickListener() {  
  12.    @Override  
  13.    public void onClick(DialogInterface dialog, int which) {  
  14.    dialog.cancel(); // Cancel the Event and back to Activity  
  15.    }  
  16. }); 

Explanation

First, we have created the instance of AlertBox.Builder then the current context as a single parameter, in other words MainActivity.

Then, the setMessage() method accepts the string content that we want to see in the Body of the Alert Box.

Along with this, we add an isCancelable() method to false.

Finally, we add two button as postivite (Yes) and nesgative (No).

Where the Positive Button will close the application (specifically, the Main Activity) and the Negative Button will remain in the same Actvity.

Hence, in Postive onClick() we have used the life-cycle's method finish() that will close the app and in a negative onClick() we have used the dialog.Cancel().

The final code will be:

  1. package com.greensyntax.alertbox;  
  2.   
  3. import android.support.v7.app.ActionBarActivity;  
  4. import android.support.v7.app.ActionBar;  
  5. import android.support.v4.app.Fragment;  
  6. import android.app.AlertDialog;  
  7. import android.app.AlertDialog.Builder;  
  8. import android.content.DialogInterface;  
  9. import android.os.Bundle;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.os.Build;  
  14.   
  15. public class MainActivity extends ActionBarActivity {  
  16.   
  17.    @Override  
  18.    protected void onCreate(Bundle savedInstanceState) {  
  19.       super.onCreate(savedInstanceState);  
  20.       setContentView(R.layout.activity_main);  
  21.   
  22.       // Instance of Alert Dialog  
  23.       AlertDialog.Builder builder=new Builder(MainActivity.this);  
  24.       builder.setMessage("WOW! I have created a DialogBox :)")  
  25.       .setCancelable(false)  
  26.       .setPositiveButton("Yes"new DialogInterface.OnClickListener() {  
  27.   
  28.       @Override  
  29.       public void onClick(DialogInterface arg0, int arg1) {  
  30.   
  31.          finish(); // Close The Activity  
  32.       }  
  33.    })  
  34.    .setNegativeButton("No"new DialogInterface.OnClickListener() {  
  35.    @Override  
  36.    public void onClick(DialogInterface dialog, int which) {  
  37.   
  38.       dialog.cancel(); // Cancel the Event and back to Activity  
  39.    }  
  40.    });  
  41.   
  42.       // Till now, we have builded a Alert Box  
  43.       // Show()  
  44.       AlertDialog alert =builder.create();  
  45.       alert.setTitle("C# Corner");  
  46.       alert.show();  
  47.    }  

Finally, we have set the title using setTitle() and called the show() method.

Now, when you render this app in the Emulator then it will be like:



Conlusion

If you experience any problem when doing it then try to solve it by Quick Fix (Ctrl+1 ). Though, if it doesn't solve yours then go for the enclosed project file.