Menu

Android – How to use Alert Dialog

If you are an Android user, then you must have experienced a pop-up dialog showing some message either success or warning. Basically, these pop-up dialog are used in forms and special action. And these pop-up dialog is known as Alert dialog in Android programming.

pukar_tech_android_alert_dialog

Today, here we are going to implement this alert dialog in our android application. Lets start with a class for Alert dialog and lets make a class with name AlertDialog.java and you can copy the code below in this file.


package np.com.pukarm.alertdialogdemo;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class AlertDialogManager {

 public void showAlertDialog(Context context, String title, String message,
 Boolean status) {

 // Setting OK Button
 AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
 alertDialog.setTitle(title);
 alertDialog.setMessage(message);

 alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
 alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int which) { 

 });

 // Showing Alert Message
 alertDialog.show();
 }
}

Now whenever, you need to show the alert dialog, then in that activity, you need to write some line of code as shown as in below:

AlertDialogManager alert = new AlertDialogManager();
alert.showAlertDialog(CurrentActivity.this, "Alert Title", "This is message", false);

I hope this will make you able to use Alert Dialog in you Android Application.

Leave a Reply

Your email address will not be published. Required fields are marked *