Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding image to Toast?

Is it possible to programmatically add an image to a toast popup?

like image 405
Praveenkumar Avatar asked Sep 27 '11 15:09

Praveenkumar


People also ask

How do you change the background on toast?

If you need to change the background of the Toast then you need to use getView(). setBackground() function. If You need to change the color of the view created then you need to use getView(). getBackground().

Can we set a custom layout for a toast?

If a simple text message isn't enough, you can create a customized layout for your toast notification. To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView (View) method.


1 Answers

Yes, you can add imageview or any view into the toast notification by using setView() method, using this method you can customize the Toast as per your requirement.

Here i have created a Custom layout file to be inflated into the Toast notification, and then i have used this layout in Toast notification by using setView() method.

cust_toast_layout.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:id="@+id/relativeLayout1"   android:background="@android:color/white">      <TextView         android:textAppearance="?android:attr/textAppearanceLarge"         android:id="@+id/textView1" android:layout_height="wrap_content"         android:layout_width="fill_parent"         android:text="PM is here"         android:gravity="center"         android:textColor="@android:color/black">     </TextView>      <ImageView         android:layout_height="wrap_content"         android:layout_width="fill_parent"         android:src="@drawable/new_logo"         android:layout_below="@+id/textView1"         android:layout_margin="5dip"         android:id="@+id/imageView1">     </ImageView>      <TextView         android:id="@+id/textView2"         android:layout_height="wrap_content"         android:layout_width="fill_parent"         android:text="This is the demo of Custom Toast Notification"         android:gravity="center"         android:layout_below="@+id/imageView1"         android:textColor="@android:color/black">     </TextView>  </RelativeLayout> 

CustomToastDemoActivity.java

LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.cust_toast_layout,      (ViewGroup)findViewById(R.id.relativeLayout1));  Toast toast = new Toast(this); toast.setView(view); toast.show(); 
like image 53
Paresh Mayani Avatar answered Sep 28 '22 10:09

Paresh Mayani