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 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 MainActivity.java file.
First, we will assign all the values of the AlertBox.Builder class.
So, we start by adding the following packages:
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
Next on onCreate state, we will add an AlertDialog.Builder instance and assign their values.
-
- AlertDialog.Builder builder=new Builder(MainActivity.this);
- builder.setMessage("WOW! I have created a DialogBox :)")
- .setCancelable(false)
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface arg0, int arg1) {
- finish();
- }
- })
- .setNegativeButton("No", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- });
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 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:
- package com.greensyntax.alertbox;
-
- import android.support.v7.app.ActionBarActivity;
- import android.support.v7.app.ActionBar;
- import android.support.v4.app.Fragment;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.os.Build;
-
- public class MainActivity extends ActionBarActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
- AlertDialog.Builder builder=new Builder(MainActivity.this);
- builder.setMessage("WOW! I have created a DialogBox :)")
- .setCancelable(false)
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface arg0, int arg1) {
-
- finish();
- }
- })
- .setNegativeButton("No", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- dialog.cancel();
- }
- });
-
-
-
- AlertDialog alert =builder.create();
- alert.setTitle("C# Corner");
- alert.show();
- }
- }
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:
Conclusion
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.