show Alert with resource id, button settings - Android android.app

Android examples for android.app:AlertDialog

Description

show Alert with resource id, button settings

Demo Code

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

public class Main {

  /**// w w  w .j a  v a  2s .  com
   * Show Alert Dialog with resource id.
   * 
   * @param context
   *          Activity context.
   * @param titleId
   *          title id
   * @param messageId
   *          message id
   */
  public static void showAlert(Context context, int titleId, int messageId) {
    Dialog dlg = new AlertDialog.Builder(context).setIconAttribute(android.R.attr.alertDialogIcon).setTitle(titleId)
        .setPositiveButton(android.R.string.ok, null).setMessage(messageId).create();

    dlg.show();
  }

  /**
   * Show Alert Dialog with String text.
   * 
   * @param context
   *          Activity context.
   * @param title
   *          Title
   * @param message
   *          Message
   */
  public static void showAlert(Context context, String title, String message) {
    Dialog dlg = new AlertDialog.Builder(context).setIconAttribute(android.R.attr.alertDialogIcon).setTitle(title)
        .setPositiveButton(android.R.string.ok, null).setMessage(message).create();

    dlg.show();
  }

  /**
   * Show Alert Dialog with two buttons.
   * 
   * @param context
   * @param titleId
   * @param messageId
   * @param positiveButtontxt
   * @param positiveListener
   * @param negativeButtontxt
   * @param negativeListener
   */
  public static void showAlert(Context context, int titleId, int messageId, CharSequence positiveButtontxt,
      DialogInterface.OnClickListener positiveListener, CharSequence negativeButtontxt,
      DialogInterface.OnClickListener negativeListener) {
    Dialog dlg = new AlertDialog.Builder(context).setIconAttribute(android.R.attr.alertDialogIcon).setTitle(titleId)
        .setPositiveButton(positiveButtontxt, positiveListener).setNegativeButton(negativeButtontxt, negativeListener)
        .setMessage(messageId).setCancelable(false).create();

    dlg.show();
  }

  /**
   * Show Alert Dialog with positive button.
   * 
   * @param context
   * @param titleId
   * @param messageId
   * @param positiveButtontxt
   * @param positiveListener
   */
  public static void showAlert(Context context, int titleId, int messageId, CharSequence positiveButtontxt,
      DialogInterface.OnClickListener positiveListener) {
    Dialog dlg = new AlertDialog.Builder(context).setIconAttribute(android.R.attr.alertDialogIcon).setTitle(titleId)
        .setPositiveButton(positiveButtontxt, positiveListener).setMessage(messageId).setCancelable(false).create();

    dlg.show();
  }

}

Related Tutorials