Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package github.nisrulz.androidutils.dialog;

/**
* Created by Pranay.
*/


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

public class DialogUtils {

private static final String TAG = "DialogUtils";
private static ProgressDialog mProgressDialog;

/**
* Shows a progress dialog with a spinning animation in it. This method must preferably called
* from a UI thread.
*
* @param ctx Activity context
* @param title Title of the progress dialog
* @param body Body/Message to be shown in the progress dialog
* @param isCancellable True if the dialog can be cancelled on back button press, false otherwise
*/
public static void showProgressDialog(Context ctx, String title, String body, boolean isCancellable) {
showProgressDialog(ctx, title, body, null, isCancellable);
}

/**
* Shows a progress dialog with a spinning animation in it. This method must preferably called
* from a UI thread.
*
* @param ctx Activity context
* @param title Title of the progress dialog
* @param body Body/Message to be shown in the progress dialog
* @param icon Icon to show in the progress dialog. It can be null.
* @param isCancellable True if the dialog can be cancelled on back button press, false otherwise
*/
public static void showProgressDialog(Context ctx, String title, String body, Drawable icon, boolean isCancellable) {

if (ctx instanceof Activity) {
if (!((Activity) ctx).isFinishing()) {
mProgressDialog = ProgressDialog.show(ctx, title, body, true);
mProgressDialog.setIcon(icon);
mProgressDialog.setCancelable(isCancellable);
}
}
}

/**
* Check if the {@link ProgressDialog} is visible in the UI.
*/
public static boolean isProgressDialogVisible() {
return (mProgressDialog != null);
}

/**
* Dismiss the progress dialog if it is visible.
*/
public static void dismissProgressDialog() {

if (mProgressDialog != null) {
mProgressDialog.dismiss();
}

mProgressDialog = null;
}

/**
* Shows an alert dialog with the OK button. When the user presses OK button, the dialog
* dismisses.
*/
public static void showAlertDialog(Context ctx, String title, String body) {
showAlertDialog(ctx, title, body, null);
}

/**
* Shows an alert dialog with OK button
*/
public static void showAlertDialog(Context ctx, String title, String body, DialogInterface.OnClickListener okListener) {

if (okListener == null) {
okListener = new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
};
}

AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(body).setPositiveButton("OK", okListener);

if (!TextUtils.isEmpty(title)) {
builder.setTitle(title);
}

builder.show();
}

/**
* Creates a confirmation dialog with Yes-No Button. By default the buttons just dismiss the
* dialog.
*
* @param ctx
* @param message Message to be shown in the dialog.
* @param yesListener Yes click handler
* @param noListener **
*/
public static void showConfirmDialog(Context ctx, String message, DialogInterface.OnClickListener yesListener, DialogInterface.OnClickListener noListener) {
showConfirmDialog(ctx, message, yesListener, noListener, "Yes", "No", true);
}

/**
* Creates a confirmation dialog with Yes-No Button. By default the buttons just dismiss the
* dialog.
*
* @param ctx
* @param message Message to be shown in the dialog.
* @param yesListener Yes click handler
* @param noListener
* @param yesLabel Label for yes button
* @param noLabel Label for no button
*/
public static void showConfirmDialog(Context ctx, String message, DialogInterface.OnClickListener yesListener, DialogInterface.OnClickListener noListener, String yesLabel, String noLabel, boolean isCancelable) {

AlertDialog.Builder builder = new AlertDialog.Builder(ctx);

if (yesListener == null) {
yesListener = new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
};
}

if (noListener == null) {
noListener = new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
};
}

builder.setCancelable(isCancelable);
builder.setMessage(message).setPositiveButton(yesLabel, yesListener).setNegativeButton(noLabel, noListener).show();
}

/**
* Creates a confirmation dialog that show a pop-up with button labeled as parameters labels.
*
* @param ctx {@link Activity} {@link Context}
* @param message Message to be shown in the dialog.
* @param dialogClickListener For e.g.
* <p/>
* @param positiveBtnLabel For e.g. "Yes"
* @param negativeBtnLabel For e.g. "No"
*/
public static void showDialog(Context ctx, String message, String positiveBtnLabel, String negativeBtnLabel, DialogInterface.OnClickListener dialogClickListener) {

if (dialogClickListener == null) {
throw new NullPointerException("Action listener cannot be null");
}

AlertDialog.Builder builder = new AlertDialog.Builder(ctx);

builder.setMessage(message).setPositiveButton(positiveBtnLabel, dialogClickListener).setNegativeButton(negativeBtnLabel, dialogClickListener).show();
}

}