Helper method for showing Alert Dialog - Android User Interface

Android examples for User Interface:Alert Dialog

Description

Helper method for showing Alert Dialog

Demo Code


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

public class Main {
    private static final int NO_RESOURCE = -1;
    public static final int NONE = -1;

    public static AlertDialog showAlert(Context context, int title,
            int message) {
        return showAlert(context, title, message, null, NO_RESOURCE, null,
                NO_RESOURCE, null);/*from w  ww . j  ava  2  s . co  m*/
    }

    public static AlertDialog showAlert(Context context, int title,
            int message, int positiveButton) {
        return showAlert(context, title, message, null, positiveButton,
                null, NO_RESOURCE, null);
    }

    public static AlertDialog showAlert(Context context, int title,
            int message, int positiveButton, OnClickListener listener) {
        return showAlert(context, title, message, null, positiveButton,
                listener, NO_RESOURCE, null);
    }

    public static AlertDialog showAlert(Context context, int title,
            int message, int positiveButton, int negativeButton) {
        return showAlert(context, title, message, null, positiveButton,
                null, negativeButton, null);
    }

    public static AlertDialog showAlert(Context context, int title,
            int message, View contentView, int positiveButton,
            int negativeButton) {
        return showAlert(context, title, message, contentView,
                positiveButton, null, negativeButton, null);
    }

    public static AlertDialog showAlert(Context context, int title,
            int message, int positiveButton,
            OnClickListener positiveListener, int negativeButton,
            OnClickListener negativeListener) {
        return showAlert(context, title, message, null, positiveButton,
                positiveListener, negativeButton, negativeListener);
    }

    public static AlertDialog showAlert(Context context, int title,
            int message, View contentView, int positiveButton,
            OnClickListener positiveListener, int negativeButton,
            OnClickListener negativeListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        if (title != NONE) {
            builder.setTitle(title);
        }

        if (message != NONE) {
            builder.setMessage(message);
        }

        if (contentView != null) {
            builder.setView(contentView);
        }

        if (positiveButton != NO_RESOURCE) {
            builder.setPositiveButton(positiveButton, positiveListener);
        }

        if (negativeButton != NO_RESOURCE) {
            builder.setNegativeButton(negativeButton, negativeListener);
        }

        return builder.show();
    }
}

Related Tutorials