Android UI How to - Show Alert Dialog








Question

We would like to know how to show Alert Dialog.

Answer

/*from w  ww .  j  a v  a  2 s . c o  m*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.res.Resources;

public class Main {
  public static void showAlert(Activity activity, String message){
    showAlert(activity, "", message);
  }

  public static void showAlert(Activity activity, int titleStringId, int messageStringId){
    Resources res = activity.getResources();
    showAlert(activity, res.getString(titleStringId), res.getString(messageStringId));    
  }

  public static void showAlert(Activity activity, int messageStringId){
    Resources res = activity.getResources();
    showAlert(activity, "", res.getString(messageStringId));    
  }
  public static void showAlert(Activity activity, String title, String message){
    AlertDialog.Builder ad = new AlertDialog.Builder(activity);
    ad.setTitle(title);
    ad.setMessage(message);
    ad.setNegativeButton("OK", null);
    ad.setCancelable(true);
    ad.show();        
  }
}