Adding an image to Toast?

Can I programmatically add an image to a toast popup?

+53
android android-toast
Sep 27 '11 at 15:44
source share
6 answers

Yes , you can add an image or any view to the notification of the toast using the setView () method, using this method, you can customize Toast according to your requirement.

Here I created a custom layout file that needs to be inflated in the Toast notification, and then I used this layout in the Toast notification using the 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(); 
+80
Sep 28 '11 at 5:41
source share

Just use the following:

 Toast toast = new Toast(myContext); ImageView view = new ImageView(myContext); view.setImageResource(R.drawable.image_icon); toast.setView(view); toast.show(); 
+19
Apr 20 '13 at 0:20
source share

You can create any view programmatically (as I assume you are asking how to do it WITHOUT using LayoutInflater) and call setView on the toast you made.

  //Create a view here LinearLayout v = new LinearLayout(this); //populate layout with your image and text or whatever you want to put in here Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(v); toast.show(); 
+14
Sep 27 '11 at 15:55
source share

Knickedi's solution is good, but if you only need the icon next to the text, you can use the fact that Toast has a predefined TextView with the same identifier and sets the icon in the TextView:

 Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT); TextView tv = (TextView) toast.getView().findViewById(android.R.id.message); if (null!=tv) { tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0); tv.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.padding_toast)); 
+7
Nov 08 '13 at
source share

There is always the opportunity to create your own layout. There was one fact that I did not like: it violates the default toast user interface. This may vary across platforms and implementations. There is no easy way to use the default system resource, so I decided to crack the toast and paste the image into it.

Hint: you can get the default resource as follows: Toast.makeToast(context, "", 0).getView().getBackground()




Here's the helper that will display the image before the toast: Helper.makeImageToast(context, R.drawable.my_image, "Toast with image", Toast.LENGTH_SHORT).show()

I use this to indicate success, information or error. Makes the toast information more beautiful and expressive ...

(It’s worth mentioning that the hack is based on the fact that the internal toast uses LinearLayout , therefore it does not depend on the system and implementation. See comments.)

 public static Toast makeImageToast(Context context, int imageResId, CharSequence text, int length) { Toast toast = Toast.makeText(context, text, length); View rootView = toast.getView(); LinearLayout linearLayout = null; View messageTextView = null; // check (expected) toast layout if (rootView instanceof LinearLayout) { linearLayout = (LinearLayout) rootView; if (linearLayout.getChildCount() == 1) { View child = linearLayout.getChildAt(0); if (child instanceof TextView) { messageTextView = (TextView) child; } } } // cancel modification because toast layout is not what we expected if (linearLayout == null || messageTextView == null) { return toast; } ViewGroup.LayoutParams textParams = messageTextView.getLayoutParams(); ((LinearLayout.LayoutParams) textParams).gravity = Gravity.CENTER_VERTICAL; // convert dip dimension float density = context.getResources().getDisplayMetrics().density; int imageSize = (int) (density * 25 + 0.5f); int imageMargin = (int) (density * 15 + 0.5f); // setup image view layout parameters LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(imageSize, imageSize); imageParams.setMargins(0, 0, imageMargin, 0); imageParams.gravity = Gravity.CENTER_VERTICAL; // setup image view ImageView imageView = new ImageView(context); imageView.setImageResource(imageResId); imageView.setLayoutParams(imageParams); // modify root layout linearLayout.setOrientation(LinearLayout.HORIZONTAL); linearLayout.addView(imageView, 0); return toast; } 
+1
Oct 24 2018-11-21T00:
source share

I think it’s better if we show the Toast text in the image we pass to the makeImageToast function ... so I highlight the Knickedi codes and:

 public class utility { public static Toast makeImageToast(Context context, int imageResId, CharSequence text, int length) { Toast toast = Toast.makeText(context, text, length); View rootView = toast.getView(); LinearLayout linearLayout = null; View messageTextView = null; // check (expected) toast layout if (rootView instanceof LinearLayout) { linearLayout = (LinearLayout) rootView; if (linearLayout.getChildCount() == 1) { View child = linearLayout.getChildAt(0); if (child instanceof TextView) { messageTextView = (TextView) child; ((TextView) child).setGravity(Gravity.CENTER); } } } // cancel modification because toast layout is not what we expected if (linearLayout == null || messageTextView == null) { return toast; } ViewGroup.LayoutParams textParams = messageTextView.getLayoutParams(); ((LinearLayout.LayoutParams) textParams).gravity = Gravity.CENTER; // convert dip dimension float density = context.getResources().getDisplayMetrics().density; int imageSize = (int) (density * 25 + 0.5f); int imageMargin = (int) (density * 15 + 0.5f); // setup image view layout parameters LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(imageSize, imageSize); imageParams.setMargins(0, 0, imageMargin, 0); imageParams.gravity = Gravity.CENTER; // setup image view ImageView imageView = new ImageView(context); imageView.setImageResource(imageResId); imageView.setLayoutParams(imageParams); // modify root layout linearLayout.setOrientation(LinearLayout.HORIZONTAL); linearLayout.setBackgroundResource(imageResId); linearLayout.setGravity(Gravity.CENTER); linearLayout.setHorizontalGravity(Gravity.CENTER); linearLayout.setHorizontalGravity(Gravity.CENTER); //addView(imageView, 0); return toast; } 

}

and this is its use:

 utility.makeImageToast(getApplicationContext(), R.drawable.your_image,"your_text",Toast.LENGTH_LONG).show(); 
0
Feb 13 '17 at 23:55
source share



All Articles