Custom Toast in Android Example with Source Code and Description

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

I know Android default Toast can’t full fill all the requirements for a developers. There are some limitations of  Android Toast API. But don’t worry. You can create your own Custom Toast in Android. Here you can find Custom Toast in Android Example with source code and detailed description. We will create 2 different type of Custom Toast in android, which can full fill all your requirements.

1. Default Toast in Android

2. Custom Toast With Duration.

3. Custom Toast With Image, Font, Style, Border etc.

Here We will discuss regarding default Toast first, then we will move to Custom Toast with Duration and finally we will discuss about Custom Toast with image font border etc.

When we are going to create a custom toast in Android, we have play with xml file and little bit of java coding. Trust me guys it is very easy to crate custom toast in android. This toast will be display without flickering. Just we need to put our logic.

1. Default Toast in Android Example

1. First import the below package into your activity java file.

import android.widget.Toast;

3. Android Toast have 2 different properties, We can show the android default toast for 2 different duration. One option  is for short duration and other  is for little long duration. We can use below properties for android default toast.

2. Custom Toast With Duration.

Steps:

1. Create a countdown timer.

3. Show the default toast ( with “Toast.LENGTH_SHORT” attribute) using countdown timer.

4. Cancel or stop the custom toast display.

So lets start our mission. Here is your code to create a countdown timer for the toast.

Code Snippet for Showing Custom Toast With Timer

CountDownTimer myTimer = null; // need to declare this variable as static inside the class,
//so when you use this snippet, don’t declare here, declare in side the class, explained below.
//so that when we will cancel the toast, we will just stop the countdown timer form another function,
//so that the toast will never show after.
final Toast tag = Toast.makeText(getBaseContext(), “YOUR MESSAGE”,Toast.LENGTH_SHORT);
tag.show();
//Create the count down timer here.  
//Duration (1000-19000)this toast will show from time 1 to 19 seconds = duration 20 seconds
myTimer =new CountDownTimer(19000, 1000)
{
public void onTick(long millisUntilFinished) {tag.show();}
public void onFinish() {tag.show();}

}.start();

 Code Snippet for Cancel Custom Toast With Timer

Tags: