Custom Toast in Android

11985 views.

Post by:  Priyanshu Kumar Chaudhary

A toast is small message displayed on the top of the content of our activity. By default, toast are displayed on the bottom of the activity.

 

Code for Default Toast:-

 

Here, the static makeText() method of Toast Class takes 3 arguments as input.

  1. Context context,
  2. CharSequence text or @StringRes int ResId
  3. @IntDef int duration

You can either show toast for short duration or long duration.

a] Toast.LENGTH_SHORT is used to display toast for about 2 seconds.

b] Toast.LENGTH_LONG is used to display toast for about 4 to 5 seconds.

Preview of Default Toast:-

In general, any popular apps do not use the default toast. They create their own custom toast & display it to users as per their requirement.

 

Steps for making Custom Toast:-

Step 1:- Create a layout named “custom.xml” with root ViewGroup id as “customid”.

Custom.xml

 

Step 2:- Make a applyCustomToast()

Step 3:- Call the applyCustomToast() as per your requirement.

applyCustomToast();

Output;

You may also like...

Leave a Reply