25

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?

Malfist
  • 31,179
  • 61
  • 182
  • 269

3 Answers3

55

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.

Dmitry
  • 1,188
  • 13
  • 17
  • 3
    Javacadabra answer is better in my opinion – rubdottocom Mar 06 '14 at 16:14
  • 1
    Am I reading the question wrong? But the question asks how to PREVENT it from being customised, and you are stating HOW to customise it? – WORMSS Feb 05 '15 at 12:21
  • @WORRMS, you're right, but... As far as theme is changed, any toast that doesn't apply this theme is a custom toast (because we need to "re-style" it back) – Dmitry Aug 13 '15 at 06:49
32

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

Community
  • 1
  • 1
Javacadabra
  • 5,578
  • 15
  • 84
  • 152
5

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>
Yilmaz Guleryuz
  • 9,313
  • 3
  • 32
  • 43