show alert Dialog and set yes/no button listener - Android User Interface

Android examples for User Interface:Alert Dialog

Description

show alert Dialog and set yes/no button listener

Demo Code


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

public class Main {
    public static void show(Context context, String message,
            String yesLabel, DialogInterface.OnClickListener yesListener,
            String noLabel, DialogInterface.OnClickListener noListener) {
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setTitle("Title");
        dialog.setMessage(message);/*from  www.j  a va  2 s  . c  o m*/
        dialog.setIcon(android.R.drawable.ic_dialog_alert);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, yesLabel,
                    yesListener);
            dialog.setButton(DialogInterface.BUTTON_NEGATIVE, noLabel,
                    noListener);
        } else {
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, noLabel,
                    noListener);
            dialog.setButton(DialogInterface.BUTTON_NEGATIVE, yesLabel,
                    yesListener);
        }

        dialog.show();
    }
}

Related Tutorials