Show an error dialog. - Android User Interface

Android examples for User Interface:Dialog

Description

Show an error dialog.

Demo Code


//package com.java2s;

import java.lang.reflect.Field;

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

public class Main {
    private static String mPackageName = null;

    /**/*ww  w . j a v a  2s.  co m*/
     * Show an error dialog.
     * 
     * @param context
     *            The current context.
     * @param title
     *            The title string id.
     * @param message
     *            The message string id.
     */
    public static void showErrorDialog(Context context, int title,
            int message) {
        String packagename = context.getPackageName();
        new AlertDialog.Builder(context)
                .setTitle(title)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setMessage(message)
                .setPositiveButton(
                        getResId("string", "browser_Commons_Ok",
                                packagename), null).show();
    }

    public static void showErrorDialog(Context context, int title,
            String message) {
        String packagename = context.getPackageName();
        new AlertDialog.Builder(context)
                .setTitle(title)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setMessage(message)
                .setPositiveButton(
                        getResId("string", "browser_Commons_Ok",
                                packagename), null).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