display Alert dialog with OK Cancel button - Android User Interface

Android examples for User Interface:Alert Dialog

Description

display Alert dialog with OK Cancel button

Demo Code


//package com.java2s;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class Main {
    public static final String OK = "OK";
    public static final String CANCEL = "Cancel";
    private static final DialogInterface.OnClickListener DISMISSER = new DialogInterface.OnClickListener() {

        @Override//from  w w w .  j  a  v  a  2 s  . co m
        public void onClick(DialogInterface dialog, int which) {

            dialog.dismiss();
        }
    };

    public static void displayAlertOKCancel(Context context,
            String alertTitle, String alertMessage,
            DialogInterface.OnClickListener okListener) {

        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle(alertTitle);
        alertDialog.setMessage(alertMessage);
        alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, OK,
                okListener);
        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, CANCEL,
                DISMISSER);
        alertDialog.show();
    }
}

Related Tutorials