Is it possible to programmatically add an image to a toast popup?
- 
                    5[Try to see here](http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView) – crbin1 Sep 27 '11 at 15:50
- 
                    1@SpK +1 for such good question – swiftBoy Jun 19 '12 at 05:53
8 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();
 
    
    - 24,084
- 23
- 95
- 173
 
    
    - 127,700
- 71
- 241
- 295
- 
                    @Blundell [**Here**](http://www.technotalkative.com/android-custom-toast-notification/) is the detailed tutorial with output snap. – Paresh Mayani Mar 19 '12 at 05:46
Simply, Use the following:
Toast toast = new Toast(myContext);
ImageView view = new ImageView(myContext); 
view.setImageResource(R.drawable.image_icon); 
toast.setView(view); 
toast.show();
 
    
    - 7,009
- 4
- 25
- 38
 
    
    - 9,874
- 8
- 48
- 66
Knickedi's solution is good, but if you only need an icon next to the text you can make use of the fact that the Toast has a pre-defined TextView with the same ID and set the icon on 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));
 
    
    - 853
- 9
- 13
You can create any view programmatically (since I am assuming you are asking on how to do this WITHOUT using a 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();
 
    
    - 13,737
- 4
- 39
- 47
I think this is better that we show text of Toast on the image which we pass to the makeImageToast function... so I shades 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 use of it:
utility.makeImageToast(getApplicationContext(),
                 R.drawable.your_image,"your_text",Toast.LENGTH_LONG).show();
 
    
    - 396
- 3
- 5
Toast aa = Toast.makeText(getBaseContext(), "OPEN",Toast.LENGTH_SHORT);
ImageView cc = new ImageView(getBaseContext());
cc.setImageResource(R.drawable.a);
aa.setView(cc);
aa.show();
 
    
    - 24,084
- 23
- 95
- 173
 
    
    - 11
- 1
- 1
- 
                    2Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to describe the essential parts of your solution. This appears to be all but identical to previous answers. – Lece Apr 15 '18 at 00:33
There's always the possibility to create a custom layout. There was one fact which I disliked about that: It breaks the system default toast UI. This could differ on different platforms and implementations. There's no simple way to use the system default resource so I decided to hack the toast and force an image into it.
Hint: You can get the default resource like this:
Toast.makeToast(context, "", 0).getView().getBackground()
Here's a helper which will display an image in front of the toast message:
Helper.makeImageToast(context, R.drawable.my_image, "Toast with image", Toast.LENGTH_SHORT).show()
I use that to indicate success, info or error. Makes a toast information nicer and more expressive...
(It's worth mentioning that the hack bases on the fact that the internal toast is using a LinearLayout so isn't system and implementation independent. 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;
}
 
    
    - 8,742
- 3
- 43
- 45
- 
                    This works only if the Toast's layout is of type `LinearLayout`. There is no contact afaik that the Toast will always have a LinearLayout. Your code deals with this by not adding the image but it's worth noting this solution is not device / version independent. – Graeme Jan 17 '12 at 17:00
- 
                    @Graeme You're right. Thank you for the hint. The better way would be to recreate the toast in an own LinearLayout. I'll update my answer when I find some time for that. – Knickedi Jan 18 '12 at 21:24
    class CustomToast extends AppCompatActivity {
    Button custom_toast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_toast);
        custom_toast = (Button) findViewById(R.id.customToast);
        custom_toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater=getLayoutInflater();
                View layout=inflater.inflate(R.layout.custom_toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
                TextView toastTextView=(TextView) findViewById(R.id.toastTextView);
                ImageView toastimageview=(ImageView) findViewById(R.id.toastImageView);
                toastTextView.setText("Custom toast in android");
                toastimageview.setImageResource(R.drawable.ic_launcher_background);
                Toast toast=new Toast(CustomToast.this);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
            }
        });
    }
}
 
    
    - 1,587
- 4
- 16
- 29
 
    
    - 131
- 1
- 8
