Toast background changing to match Activity's Theme
I have created a custom theme for my activities that they all use. In the theme, I set android:background, and this h开发者_如何学运维appens to cause any dialog or toast message to look very strange.
How do I prevent toast and the other dialogs from absorbing the theme's properties?
You can easily create custom toast by the following code:
Toast toast = Toast.makeText(context, resTxtId, Toast.LENGTH_LONG);
View view = toast.getView();
view.setBackgroundResource(R.drawable.custom_bkg);
TextView text = (TextView) view.findViewById(android.R.id.message);
/*here you can do anything with text*/
toast.show();
Or you can instantiate a completely custom toast:
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_layout, null);
toast.setView(view);
toast.show();
Dialog customizing is a more complex routine. But there is similar workaround.
I realise the question has been answered and the post is quite old at this stage. However I thought I would leave an answer for those who come across this question.
I ran into trouble with this issue today and the way I resolved it was by displaying my Toast messages like this:
Toast.makeText(getApplicationContext(), "Checking login details...", Toast.LENGTH_SHORT).show();
As opposed to this (presuming the message is being called from within a View):
Toast.makeText(v.getContext(), "Checking login details...", Toast.LENGTH_SHORT).show();
It cleared up the issues I was having. Anyways hope it helps. Here is link to my question on similar topic.
Toast background color being changed
here comes complete example, to be used for customized Toast across activities.
displayToast
// display customized Toast message
public static int SHORT_TOAST = 0;
public static int LONG_TOAST = 1;
public static void displayToast(Context caller, String toastMsg, int toastType){
try {// try-catch to avoid stupid app crashes
LayoutInflater inflater = LayoutInflater.from(caller);
View mainLayout = inflater.inflate(R.layout.toast_layout, null);
View rootLayout = mainLayout.findViewById(R.id.toast_layout_root);
ImageView image = (ImageView) mainLayout.findViewById(R.id.image);
image.setImageResource(R.drawable.img_icon_notification);
TextView text = (TextView) mainLayout.findViewById(R.id.text);
text.setText(toastMsg);
Toast toast = new Toast(caller);
//toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setGravity(Gravity.BOTTOM, 0, 0);
if (toastType==SHORT_TOAST)//(isShort)
toast.setDuration(Toast.LENGTH_SHORT);
else
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(rootLayout);
toast.show();
}
catch(Exception ex) {// to avoid stupid app crashes
Log.w(TAG, ex.toString());
}
}
and toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#DAAA"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
精彩评论