Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Customise Toast in Android?

Tags:

android

toast

Is it possible to make Customize Toast in Android. like if can we place in it image icon and place button.

like image 669
CoDe Avatar asked Jul 03 '11 09:07

CoDe


3 Answers

You can also use the regular makeText() and handle the getView() to set an image next to see the next.

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));
like image 120
robUx4 Avatar answered Sep 21 '22 11:09

robUx4


You can put any view in a Toast using setView. However, I'm not quite sure why you would want to place a button in it, as a Toast will rapidly disappear. Taken from the officiel developer site :

When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see.

So the toast should only be used to display information. For more complex interactions, you can use a Dialog.

like image 42
Gregory Avatar answered Sep 19 '22 11:09

Gregory


Toast is non focus able.Adding button did not make sense. However you can display information.You can also control its visibility means u can hide and show by making few changes in Toast class.

like image 25
Mohit Avatar answered Sep 19 '22 11:09

Mohit