Skip to content

Commit b302b44

Browse files
committed
Move ToastUtils to Utils
1 parent a0e973c commit b302b44

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.wordpress.android.util;
2+
3+
import android.content.Context;
4+
import android.view.Gravity;
5+
import android.widget.Toast;
6+
7+
/**
8+
* Provides a simplified way to show toast messages without having to create the toast, set the
9+
* desired gravity, etc.
10+
*/
11+
public class ToastUtils {
12+
public enum Duration {SHORT, LONG}
13+
14+
private ToastUtils() {
15+
throw new AssertionError();
16+
}
17+
18+
public static void showToast(Context context, int stringResId) {
19+
showToast(context, stringResId, Duration.SHORT);
20+
}
21+
22+
public static void showToast(Context context, int stringResId, Duration duration) {
23+
showToast(context, context.getString(stringResId), duration);
24+
}
25+
26+
public static void showToast(Context context, String text) {
27+
showToast(context, text, Duration.SHORT);
28+
}
29+
30+
public static void showToast(Context context, String text, Duration duration) {
31+
Toast toast = Toast.makeText(context, text,
32+
(duration == Duration.SHORT ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG));
33+
toast.setGravity(Gravity.CENTER, 0, 0);
34+
toast.show();
35+
}
36+
}

0 commit comments

Comments
 (0)