Display a standard Ok / Cancel dialog. - Android User Interface

Android examples for User Interface:Dialog

Description

Display a standard Ok / Cancel 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  ww  w. ja  v  a2 s.  c  o  m*/
     * Display a standard Ok / Cancel 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 showOkCancelDialog(Context context, int icon,
            String title, String message,
            DialogInterface.OnClickListener onYes) {
        String packagename = context.getPackageName();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(true);
        builder.setIcon(icon);
        builder.setTitle(title);
        builder.setMessage(message);

        builder.setInverseBackgroundForced(true);
        builder.setPositiveButton(
                getResId("string", "browser_Commons_Ok", packagename),
                onYes);
        builder.setNegativeButton(
                getResId("string", "browser_Commons_Cancel", 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