Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.support.annotation.StringRes;
- import android.widget.Toast;
- /**
- * Created by simon on 2017-07-26.
- *
- * Displays a progress wheel with a optional message.
- *
- * Displays different alert styles (short toast, long toast or OK dialog)
- * based on the message's length.
- */
- public class Alert {
- private static final int MAX_MESSAGE_LENGTH_SHORT_TOAST = 25;
- private static final int MAX_MESSAGE_LENGTH_LONG_TOAST = 75;
- private ProgressDialog dialog;
- private Context context;
- public Alert(Context context) {
- this.context = context;
- }
- public void progress() {
- progress("");
- }
- public void progress(@StringRes int messageRes) {
- progress(context.getString(messageRes));
- }
- public void progress(String message) {
- dialog = new ProgressDialog(context);
- dialog.setCancelable(false);
- if (message.length() > 0) {
- dialog.setMessage(message);
- }
- dialog.show();
- }
- public void dismiss() {
- if (dialog != null) {
- dialog.dismiss();
- dialog = null;
- }
- }
- public void info(@StringRes int messageRes) {
- String message = context.getString(messageRes);
- info(message);
- }
- public void info(String message) {
- int messageLength = message.length();
- if (messageLength <= MAX_MESSAGE_LENGTH_SHORT_TOAST) {
- Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
- } else if (messageLength <= MAX_MESSAGE_LENGTH_LONG_TOAST) {
- Toast.makeText(context, message, Toast.LENGTH_LONG).show();
- } else {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setMessage(message);
- builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.dismiss();
- }
- });
- builder.create().show();
- }
- }
- public static void info(Context context, @StringRes int messageRes) {
- new Alert(context).info(messageRes);
- }
- public static void info(Context context, String message) {
- new Alert(context).info(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement