customer_toast_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="30dp"/>
<solid
android:color="#88000000"/>
</shape>
R.layout.customer_toast
?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/customer_toast_bg"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:shadowColor="#BB000000"
android:shadowRadius="2.75"
android:textColor="#FFFFFF" />
</FrameLayout>
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);
}
}