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

Toast Image

Can we show an image as a toast message?

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 -)

Thank you.

3 Answers

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.

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

Thanks Eric and Boris,

You're welcome!