Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. import android.app.AlertDialog;
  2. import android.app.ProgressDialog;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.support.annotation.StringRes;
  6. import android.widget.Toast;
  7.  
  8. /**
  9. * Created by simon on 2017-07-26.
  10. *
  11. * Displays a progress wheel with a optional message.
  12. *
  13. * Displays different alert styles (short toast, long toast or OK dialog)
  14. * based on the message's length.
  15. */
  16.  
  17. public class Alert {
  18.  
  19. private static final int MAX_MESSAGE_LENGTH_SHORT_TOAST = 25;
  20. private static final int MAX_MESSAGE_LENGTH_LONG_TOAST = 75;
  21.  
  22. private ProgressDialog dialog;
  23. private Context context;
  24.  
  25. public Alert(Context context) {
  26. this.context = context;
  27. }
  28.  
  29. public void progress() {
  30. progress("");
  31. }
  32.  
  33. public void progress(@StringRes int messageRes) {
  34. progress(context.getString(messageRes));
  35. }
  36.  
  37. public void progress(String message) {
  38. dialog = new ProgressDialog(context);
  39. dialog.setCancelable(false);
  40.  
  41. if (message.length() > 0) {
  42. dialog.setMessage(message);
  43. }
  44.  
  45. dialog.show();
  46. }
  47.  
  48. public void dismiss() {
  49. if (dialog != null) {
  50. dialog.dismiss();
  51. dialog = null;
  52. }
  53. }
  54.  
  55. public void info(@StringRes int messageRes) {
  56. String message = context.getString(messageRes);
  57. info(message);
  58. }
  59.  
  60. public void info(String message) {
  61. int messageLength = message.length();
  62.  
  63. if (messageLength <= MAX_MESSAGE_LENGTH_SHORT_TOAST) {
  64. Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
  65. } else if (messageLength <= MAX_MESSAGE_LENGTH_LONG_TOAST) {
  66. Toast.makeText(context, message, Toast.LENGTH_LONG).show();
  67. } else {
  68. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  69. builder.setMessage(message);
  70. builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
  71. public void onClick(DialogInterface dialog, int id) {
  72. dialog.dismiss();
  73. }
  74. });
  75. builder.create().show();
  76. }
  77. }
  78.  
  79. public static void info(Context context, @StringRes int messageRes) {
  80. new Alert(context).info(messageRes);
  81. }
  82.  
  83. public static void info(Context context, String message) {
  84. new Alert(context).info(message);
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement