Alert Box in Android


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.
Step 1
Before you start, create an Android project successfully (Alt+Shifty+N).
Step 2
In this demonstration, we will not make any changes to 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 file.
First, we will assign all the values of the AlertBox.Builder class.
So, we start by adding the following packages:
  1. import;  
  2. import; 
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. }); 
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 buttons as positive (Yes) and negative (No).
Where the Positive Button will close the application (specifically, the Main Activity) and the Negative Button will remain in the same Activity.
Hence, in Positive 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;  
  3. import;  
  4. import;  
  5. import;  
  6. import;  
  7. import;  
  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;  
  15. public class MainActivity extends ActionBarActivity {  
  17.    @Override  
  18.    protected void onCreate(Bundle savedInstanceState) {  
  19.       super.onCreate(savedInstanceState);  
  20.       setContentView(R.layout.activity_main);  
  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() {  
  28.       @Override  
  29.       public void onClick(DialogInterface arg0, int arg1) {  
  31.          finish(); // Close The Activity  
  32.       }  
  33.    })  
  34.    .setNegativeButton("No"new DialogInterface.OnClickListener() {  
  35.    @Override  
  36.    public void onClick(DialogInterface dialog, int which) {  
  38.       dialog.cancel(); // Cancel the Event and back to Activity  
  39.    }  
  40.    });  
  42.       // Till now, we have builded a Alert Box  
  43.       // Show()  
  44.       AlertDialog alert =builder.create();  
  45.       alert.setTitle("C# Corner");  
  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:


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.