javaandroidtoast

Why my simple Toast doesn't want to appear and the app crashes?


I everyone, I am trying to make such a dumb thing: center the text inside a Toast which is created inside an onclick event listener.

I have created a TextView inside the layout I use and when I try to call the event that should the display the toast the app crashes with this exception:

03-19 23:20:40.258: E/AndroidRuntime(3364): java.lang.IllegalArgumentException: View not   
attached to window manager

This is my code:

toastTextView = (TextView) findViewById(R.id.toastView); 
Toast toast = Toast.makeText(MyActivity.this, "Very very very long long text that should be displayed in the middle of this toast", 
                           Toast.LENGTH_SHORT);
                toast.setView(toastTextView);
                toast.show();

The xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="fill_parent"
          android:layout_height="fill_parent">

<LinearLayout android:id="@+id/chart" android:orientation="horizontal"
              android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1"/>

<TextView
    android:id="@+id/toastView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout android:orientation="horizontal"
              android:layout_width="fill_parent" android:layout_height="wrap_content">

</LinearLayout>

Where's the problem with this simple code?

Thanks for the attention!

EDIT: this is the layout of the custom_toast.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >

</LinearLayout>

Solution

  • You are using a view that is already attached to another view hierarchy. You have to inflate your custom toast view separately like this:

    custom_toast.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/black"
                    android:padding="5dp">
    
            <TextView
                    android:id="@+id/tvToast"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"/>
    
    </RelativeLayout>
    

    And that's how you use it:

    LayoutInflater inflater = LayoutInflater.from(context);
    View customToastView = inflater.inflate(R.layout.custom_toast, null);
    
    TextView tvToast = (TextView) customToastView.findViewById(R.id.tvToast);
    tvToast.setText("This is a custom toast with centered text");
    
    Toast toast = new Toast(context);
    toast.setView(customToastView);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.show();