Using:
ToastUtils.LONG.show(context, R.string.greeting);
Code:
public class ToastUtils {
public static ToastUtils LONG = new ToastUtils(Toast.LENGTH_LONG);
public static ToastUtils SHORT = new ToastUtils(Toast.LENGTH_SHORT);
private final int duration;
private ToastUtils(int duration) {
this.duration = duration;
}
public Toast makeText(Context context, String text) {
return Toast.makeText(context, text, duration);
}
public void show(Context context, String text) {
makeText(context, text).show();
}
public Toast makeText(Context context, int resId) {
return Toast.makeText(context, resId, duration);
}
public void show(Context context, int resId) {
makeText(context, resId).show();
}
}
In my opinion, there are some disadvantages of using Toast
class directly:
- In
Toast
class I often miss the call toshow()
- Parameter
length
has typeint
. So a compiler couldn't detect invalid argument value
What do you think? Maybe this class is useless?
Can you suggest any improvements?