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>
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();
Try using a SpannableString to format your Toast, you can get a lot of mileage out of that without worrying about custom views.
String fancyText = "MyFancy long text";
SpannableStringBuilder span = new SpannableStringBuilder(fancyText);
span.setSpan(new AlignmentSpan(Layout.Alignment.ALIGN_CENTER), 0, fancyText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Toast.makeText(MyActivity.this, span, Toast.LENGTH_SHORT).show();
My guess is that you have to set the toast_layout.xml as the view of your toast, not only the single textView out of it. Try to add an id to your outermost linear layout, ...
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="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout .../>
<TextView .../>
</LinearLayout>
... inflate it with the layout inflater and set this as your toast view.
java.file
View toastRootView = getLayoutInflater().inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = findViewById(R.id.toastView);
text.setText("Very [...] long text [...]");
Toast toast = new Toast(MyActivity.this)
.setView(toastRootView)
.setDuration(Toast.LENGTH_SHORT)
.show();
So "toast_layout" is the name of the xml file containing the layout and toast_layout_root is the id of the root element in this file (the outermost linear layout).
Hope this helps,
Kai
Follow up link:
http://meddington.hubpages.com/hub/How-To-Toast-In-Android
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.