Display a standard yes / no dialog. - Android User Interface

Android examples for User Interface:Dialog

Description

Display a standard yes / no dialog.

Demo Code


//package com.java2s;

import java.lang.reflect.Field;

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

public class Main {
    private static String mPackageName = null;

    /**/*from w w  w .  j a v a  2  s.c  o  m*/
     * Display a standard yes / no dialog.
     * 
     * @param context
     *            The current context.
     * @param icon
     *            The dialog icon.
     * @param title
     *            The dialog title.
     * @param message
     *            The dialog message.
     * @param onYes
     *            The dialog listener for the yes button.
     */
    public static void showYesNoDialog(Context context, int icon,
            int title, int message, DialogInterface.OnClickListener onYes) {
        String packagename = context.getPackageName();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(icon);
        builder.setTitle(context.getResources().getString(title));
        builder.setMessage(context.getResources().getString(message));

        builder.setInverseBackgroundForced(true);
        builder.setPositiveButton(
                getResId("string", "browser_Commons_Yes", packagename),
                onYes);
        builder.setNegativeButton(
                getResId("string", "browser_Commons_No", packagename),
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }

    public static Integer getResId(String rType, String rName,
            String packagename) {
        Object localObject = null;
        ;
        try {
            Class localClass = Class.forName(packagename + ".R" + "$"
                    + rType);
            Field localField = localClass.getField(rName);
            localObject = localField.get(localClass.newInstance());
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return Integer.valueOf(Integer.parseInt(localObject.toString()));
    }

    public static Integer getResId(String rType, String rName) {
        return getResId(rType, rName, mPackageName);
    }
}

Related Tutorials