Android UI How to - Set Positive and Nagative Button








Question

We would like to know how to set Positive and Nagative Button.

Answer

/*from  w  w w  .j a  v  a  2  s.  c  om*/
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

class AlertUtils {
    public static void showMessageDialog(Context context, int titleId, int messageId) {
        Dialog d = new AlertDialog.Builder(context)
            .setTitle(titleId)
            .setPositiveButton(android.R.string.ok, null)
            .setMessage(messageId)
            .create();
        d.show();
    }

    public static void showAlert(Context context, int titleId, int messageId, 
            int positiveButtonStrId, DialogInterface.OnClickListener positiveListener, 
            int negativeButtonStrId, DialogInterface.OnClickListener negativeListener) {
        Dialog d = new AlertDialog.Builder(context)
            .setTitle(titleId)
            .setMessage(messageId)
            .setCancelable(false)
            .setPositiveButton(positiveButtonStrId, positiveListener)
            .setNegativeButton(negativeButtonStrId, negativeListener)
            .create();
        d.show();
    }
}