customer_toast_bg
R.layout.customer_toast
?xml version="1.0" encoding="utf-8"?>
public class CustomerToast {
private static Toast mToast;
private static final String TAG = "CustomerToast";
private static Toast initToast(Context context, CharSequence text, int duration) {
View v = initView(context, text.toString());
Toast toast = new Toast(context.getApplicationContext());
toast.setDuration(duration);
toast.setView(v);
toast.setGravity(Gravity.CENTER, 0, 0);
return toast;
}
public static void show(final Context context, final CharSequence text, final int duration) {
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
showInUIThread(context, text, duration);
} else {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
showInUIThread(context, text, duration);
}
});
}
}
private static void showInUIThread(Context context, CharSequence text, int duration) {
if (!TextUtils.isEmpty(text)) {
if (mToast == null) {
LogUtil.d(TAG, "mToast == null");
mToast = initToast(context, text, duration);
mToast.show();
} else {
LogUtil.d(TAG, "mToast != null");
mToast.cancel();
mToast = initToast(context, text, duration);
// View v = initView(context, text.toString());
// mToast.setView(v);
mToast.show();
}
} else {
LogUtil.e(TAG, "text is empty");
}
}
public static void show(Context context, int textId, int duration) {
String text = context.getString(textId);
if (!TextUtils.isEmpty(text)) {
if (mToast == null) {
LogUtil.d(TAG, "mToast == null");
mToast = initToast(context, text, duration);
mToast.show();
} else {
LogUtil.d(TAG, "mToast != null");
mToast = initToast(context, text, duration);
mToast.show();
}
} else {
LogUtil.e(TAG, "text is empty");
}
}
private static View initView(Context context, String text) {
View v = LayoutInflater.from(context).inflate(R.layout.customer_toast, null);
TextView textView = (TextView) v.findViewById(R.id.message);
textView.setText(text);
return v;
}
}
public class ToastUtil {
public static void toastNetworkError() {
toastNormal(ASApplication.getInstance(), ASApplication.getInstance().getString(R.string.network_error));
}
public static void toastNormal(Context context, String content) {
CustomerToast.show(context.getApplicationContext(), content, Toast.LENGTH_SHORT);
}
public static void toastLong(Context context, String content) {
CustomerToast.show(context, content, Toast.LENGTH_LONG);
}
public static void toastLongNormal(Context context, String content) {
CustomerToast.show(context, content, Toast.LENGTH_LONG);
}
public static void toastNormal(String content) {
CustomerToast.show(ASApplication.getAppContext(), content, Toast.LENGTH_SHORT);
}
public static void toastNormal(int content) {
CustomerToast.show(ASApplication.getAppContext(), content, Toast.LENGTH_SHORT);
}
}