Cannot show image in toast
I want to show an image in a Toast in Android. In my layout.xml, I have defined a LinearLayout 'svllid', that contains a textview and a tablelayout, like so:
<?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"
android:background="#CCCCCC"
android:weightSum="1" android:id="@+id/llid">
<EditText
android:id="@+id/cpuText"
android:hint="Enter cpu"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="@+id/ramText"
android:hint="Enter ram"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="@+id/bandwidthText"
android:hint="Enter bandwidth"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="@+id/imageRequestButton"
android:layout_height="wrap_content"
android:text="Download"
android:layout_width="fill_parent" android:onClick="sendImageRequest">
</Button>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:weightSum="1" android:id="@+id/开发者_C百科svllid">
<TextView android:text="client profile"
android:id="@+id/profileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#000000">
</TextView>
<TableLayout
android:paddingBottom="3pt"
android:background="#0000FF"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content">
<TableRow>
<TextView
android:paddingLeft="3pt"
android:paddingTop="3pt"
android:text="Image Name"
android:layout_width="150px"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:paddingLeft="3pt"
android:text="blah.png"
android:textColor="#FFFFFF"
android:layout_width="315px"
android:layout_height="wrap_content" android:id="@+id/imageName"/>
</TableRow>
<TableRow>
<TextView
android:paddingLeft="3pt"
android:text="Size"
android:layout_width="150px"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:paddingLeft="3pt"
android:text="155kb"
android:textColor="#FFFFFF"
android:layout_width="300px"
android:layout_height="wrap_content" android:id="@+id/imageSize"/>
</TableRow>
<TableRow>
<TextView
android:paddingLeft="3pt"
android:text="Dimensions"
android:layout_width="150px"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:paddingLeft="3pt"
android:text="250 X 150px"
android:textColor="#FFFFFF"
android:layout_width="300px"
android:layout_height="wrap_content" android:id="@+id/imageDimension"/>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
I want to show the LinearLayout with id 'svllid' in a Toast and show the toast from my activity code.
Now, in the actual android code, I first call
setContentView(R.layout.main);
Subsequently, I read an image from a soap message. I then create an ImageView and I want to insert it into the LinearLayout 'svllid' and show that LinearLayout in an Android Toast.
Toast imageToast = new Toast(this);
LinearLayout toastLayout = (LinearLayout) findViewById(R.id.svllid);
toastLayout.addView(image,1);
imageToast.setView(toastLayout);
imageToast.setDuration(Toast.LENGTH_LONG);
imageToast.show();
However, it won't work. My application crashes with the error:
java.lang.IllegalArgumentException: View not attached to window manager
Any ideas why?
I believe you have to use a specific ID for the root layout. You have to name it "toast_layout" or "toast_layout_root" (not sure which one has to be used- the documentation is slightly ambigious there, try both).
And as Varun said you need to put the layout in its own layout file.
Read the Android Doc's and follow the given example.
For this you can use custom Toast to display image.
Refer This.
精彩评论