Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (2014) Testing and Debugging Making a Toast

Chaitanya chowdary
Chaitanya chowdary
2,045 Points

Toast Image

Can we show an image as a toast message?

Mohammad Abdirashid
Mohammad Abdirashid
8,211 Points

It is a good practice and makes you a better programming to always know how to read and understand the API classes. If you see this link: http://developer.android.com/reference/android/widget/Toast.html#Toast(android.content.Context)

I didn't see any method in the Toast class that let's you add ImageView. The only method that is close you can try is setView(View view). But I think this represents the Toast View itself. Update us, if you figured it out -)

3 Answers

Eric De Wildt
PLUS
Eric De Wildt
Courses Plus Student 13,077 Points

This is what you want, I found this earlier today on stack overflow.

LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.cust_toast_layout1(ViewGroup) findViewById(R.id.relativeLayout1));
    Toast toast = new Toast(this);
    toast.setView(view);
    toast.show();

You just need to customize the layout file.

Boris Vukobrat
Boris Vukobrat
4,248 Points

Any View object can be shown as toast.

Make .xml layout file with any elements you want. In code, create view by inflating layout, as Eric posted.
You are using inflate(int, ViewGroup) method, where first parameter is reference to a layout file, and the second is reference to a viewGroup that you will be showing in toast. So your viewGroup must have @+id tag