Last active
January 30, 2019 07:10
-
-
Save pranaypatel512/7438e25df355873c4aeeced214898c2e to your computer and use it in GitHub Desktop.
Show different type of dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = makeLogTag(DialogUtils.class); | |
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"); | |
} | |
/** | |
* 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) { | |
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.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" | |
* | |
* <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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment