Custom Toast in Android Example with Source Code and Description

Custom Toast in Android Example with Source Code and Description

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.

2. Then add the below line where you want to show the 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

 Code Snippet for Cancel Custom Toast With Timer

 Here is the complete code for  Custom Toast with Timer.

We have used 2 buttons for the Custom Toast project. One for showing the Toast and another for Cancel the Toast.

  • Create new android project [File >> New >> Android Project] with Project Name: CustomToastExample
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.example.customtoast’
  • Click finish

Layout creation:
We need to create one layout, which hold two buttons for test the Custom Toast application. one is for showing the custom test and other is for cancel the custom toast.

Default Application layout – main.xml:

Open main.xml under /res/layout and replace it with the below XML.

Now coming to Java code (CustomToastExampleActivity.java)

Here we create the Custom Toast in side the onCreate method of the activity. I mean inside your button click code.

Then build the app and run it on emulator or device and enjoy your custom toast.

custom-toast-in-android

__________________________________________________________________________

 

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

Now coming to the second type of custom toast. Here we will develop a simple android activity which will have one button. On clicking the button, your custom toast message will be displayed.

Steps:

1. Create an xml layout which contains a button, We will display the custom toast on the button click option.

2. Create a xml layout for our custom toast, we can add extra styles later in this xml as per our requirement.

3. A small java code snippet to display the custom toast.

Project Creation

  • Create new android project [File >> New >> Android Project] with Project Name: CustomToast
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.techblogon.customtoast’
  • Click finish

Layout Creation

We need to create two layouts:

  1. Layout of the application – main.xml
  2. Layout of the custom toast message – custom_toast.xml

1. Default Application layout – main.xml:

 Open main.xml under /res/layout and add replace it with the below XML.

 2. Layout of the custom toast message – custom_toast.xml

Create a new layout XML under /res/layout with name custom_toast.xml and the below XML. You can add/replace below code in your custom_toast.xml file.

Now We need to inflate the custom_toast xml into Toast view in java code. Below code snippet does the job of creating and showing custom toast.

Now coming to Java code (CustomToastActivity.java)

Find entire java code snippet for showing  the toast below. You can replace your CustomToastActivity.java file with below code snippet.

Then build the app and run it on emulator or device and enjoy your custom toast.

custom-toast-in-android

I hope this small tutorial will help you at it’s best. Please reply us if you will face any issue during development of custom toast. We will respond back you asap.

 

Post By SmrutiRanjan (54 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon

Connect

5 Responses to Custom Toast in Android Example with Source Code and Description

  1. AndroidExample says:

    very good post, nice dude……….

  2. Hengki says:

    I really need your help for Toast,

    I have a button in myToast it can click or clickable in ICS but in Jelly Beans listener for this button does not triggering can you solved my problem?

    • SmrutiRanjan says:

      Dear Hengki,
      You can try below tricks, it might help you.
      add the click event in the xml file rather than in the java code.

      Then add the below button click code in your java file
      //this function will be called when you click on the button
      //This OnClick option has been set in the xlm file
      public void onClickButtonToast(View V){
      //add your stuff here
      }

  3. javad says:

    thanks, it’s very gooooood ;)

  4. Faisal Goraya says:

    Nice Dude and keep it up…

Leave a Reply

Your email address will not be published. Required fields are marked *


− one = 3

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">