Toast background changing to match Activity's Theme

Malfist picture Malfist · Aug 11, 2011 · Viewed 31.7k times · Source

I have created a custom theme for my activities that they all use. In the theme, I set android:background, and this happens 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?

Answer

Dmitry picture Dmitry · Mar 28, 2012

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.