-
Notifications
You must be signed in to change notification settings - Fork 4
/
DialogUtils.java
137 lines (125 loc) · 4.71 KB
/
DialogUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.bytesizebit.androidutils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
/***********
* Android Utils
* Created by Shahar Barsheshet on 17/10/2015.
* bytesizebit@gmail.com
* www.bytesizebit.com
***********/
public class DialogUtils {
private DialogUtils() {
}
/**
* Show an Alert Dialog with one button
*
* @param activity the context
* @param title dialog title
* @param text dialog message
* @param buttonText text to appear in the dialog button
* @param buttonListener button onClick callback
* @return the AlertDialog
*/
public static AlertDialog showOneButtonsDialog(
final Activity activity, final String title, final String text, final String buttonText, final DialogInterface.OnClickListener buttonListener) {
if (activity == null || activity.isFinishing()) {
return null;
}
return new AlertDialog.Builder(activity).setTitle(title)
.setMessage(text)
.setPositiveButton(buttonText, buttonListener)
.show();
}
/**
* Show an Alert Dialog with two buttons
*
* @param activity the context
* @param title dialog title
* @param text dialog message
* @param negativeButtonText negative text
* @param leftButtonListener negative button callback
* @param positiveButtonText positive button text
* @param positiveButtonListener positive button text
* @return the AlertDialog
*/
public static AlertDialog showTwoButtonsDialog(
final Activity activity, final String title, final String text, final String negativeButtonText, final DialogInterface.OnClickListener leftButtonListener, final String
positiveButtonText, final DialogInterface.OnClickListener positiveButtonListener) {
if (activity == null || activity.isFinishing()) {
return null;
}
return new AlertDialog.Builder(activity).setTitle(title)
.setMessage(text)
.setPositiveButton(negativeButtonText, leftButtonListener)
.setNegativeButton(positiveButtonText, positiveButtonListener)
.show();
}
/**
* Displays a dialog box with an OK button
*
* @param activity the context
* @param title dialog title
* @param text dialog message
* @param okListener button callback
*/
public static void showOkDialog(final Activity activity, final String title, final String text, final DialogInterface.OnClickListener okListener) {
if (activity == null || activity.isFinishing()) {
return;
}
tryOnUiThread(activity, new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(activity).setTitle(title).setMessage(text).setPositiveButton(android.R.string.ok, okListener).show().setOwnerActivity(activity);
}
});
}
/**
* Displays a dialog box with an OK button
*
* @param activity the context
* @param title dialog title
* @param text dialog message
*/
public static void showOkDialog(final Activity activity, final String title, final String text) {
showOkDialog(activity, title, text, null);
}
/**
* Displays a dialog box with an OK button
*
* @param activity the context
* @param title dialog title
* @param text dialog message
* @param okListener button callback
* @return the SlertDialog
*/
public static AlertDialog createOkDialog(final Activity activity, final String title, final String text, final DialogInterface.OnClickListener okListener) {
AlertDialog dialog = new AlertDialog.Builder(activity).setTitle(title).setMessage(text).setPositiveButton(android.R.string.ok, okListener).create();
dialog.setOwnerActivity(activity);
return dialog;
}
/**
* Dismiss dialog safely
*
* @param dialog dialog to dismiss
*/
public static void dismissDialogSafely(Dialog dialog) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
// helper to run on the UI thread
private static void tryOnUiThread(Activity activity, final Runnable runnable) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
runnable.run();
} catch (Exception e) {
// probably window was closed
}
}
});
}
}