user2446494 user2446494 - 4 years ago 121
Android Question

How to Customize Toast In Android

I am new to Android and working on a sample app. I want to learn how can we customize the default Android Toast.
I want to change the color, Style and other attribute of Toast.

Can we add Image in Toast as well ?

I read following post on stackOverflow

How to Customise Toast in Android?.
customize toast in android

but none of these explains how to add image in Toast.

Answer Source

Yes we can change the color, size , position, and other attributes of Toast. We can also add an Image to Toast.

A good blog for this How To Customize Toast In Android All the contents are taken from this blog

You can create an XML and inflate it Toast.

You can also do it at run time

    LinearLayout  layout=new LinearLayout(this);

    TextView  tv=new TextView(this);
    // set the TextView properties like color, size etc


    // set the text you want to show in  Toast
    tv.setText("My Custom Toast at Bottom of Screen");  

   ImageView   img=new ImageView(this);

    // give the drawble resource for the ImageView

  // add both the Views TextView and ImageView in layout

    Toast toast=new Toast(this); //context is object of Context write "this" if you are an Activity
   // Set The layout as Toast View

      // Position you toast here toast position is 50 dp from bottom you can give any integral value   
     toast.setGravity(Gravity.BOTTOM, 0, 50);;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download