See link here . You find your solution. And try:
Create a custom toast view
If a simple text message is not enough, you can create an individual layout for your toast. To create a custom layout, define the layout of the view in XML or application code and pass the root View object to the setView (View) method.
For example, you can create a breadboard for the toast, visible in the screenshot to the right, with the following XML (saved as toast_layout.xml):
<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>
Note that the identifier of the LinearLayout element is "toast_layout". You should use this ID to inflate the layout from XML, as shown below:
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello! This is a custom toast!"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();
First retrieve LayoutInflater using getLayoutInflater () (or getSystemService ()) and then inflate the layout from XML using inflate (int, ViewGroup). The first parameter is the layout resource identifier, and the second is the root view. You can use this bloated layout to find more View objects in the layout, so now create and define content for ImageView and TextView elements. Finally, create a new Toast with Toast (Context) and set some toast properties, such as gravity and duration. Then call setView (View) and pass it the bloated layout. Now you can display the toast using a custom layout by calling show ().
Note. Do not use the public constructor for Toast unless you intend to define a layout using setView (View). If you don't have a custom layout, you should use makeText (Context, int, int) to create a Toast.
Deepak Swami Jul 02 '12 at 6:11 2012-07-02 06:11
source share