Android Custom Toast

Android Custom Toast

What is Toast?

A Toast is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user’s current activity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events.

Toast is transient notifications that stay visible for less than a few seconds before fading out. Toast don’t take focus and its not a modal , so that they don’t interrupt the application.You can also use it for debugging purpose. A toast provides simple feedback about an operation in a small popup.

Toasts are excellent for informing your users of events without forcing them to open an Activity or browse a Notification. they provide a perfect mechanism for alerting users to events occurring in background Services without interrupting foreground applications. Generally, your application should show Toasts only if one of its Activities is active.

In our earlier tutorials, we usually see toast as a simple text message which pops up on the screen. But we can create custom toast with images and more style attributes like padding, gravity, etc.

Example

In this tutorial, we are going to learn how to create a Custom Toast with Image and Text with different gravity.

VIDEO DEMO

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. This are some strings that i am going to use in this tutorial.

3. Firstly, create a main activity xml layout naming activity_main.xml that contains one textview and one button that shows toast on click.

4. Now, for custom toast we have to create a new xml layout file naming custom_toast.xml. In this layout we can put any view that we want for our custom toast to show. I am using a TextView and ImageView.

5. Now, we create a method with int parameter for Gravity. In this method we inflate a new layout and set the view over Toast. This is very simple to use you can create any xml layout and use this method to show custom toast.

6. Finally come to your MainActivity.java and add the following code. In this code an AlertDialog will be shown on Button press that display three option to choose gravity “TOP”, “CENTRE” and “BOTTOM”.  After selecting gravity the custom toast will be called to show custom toast with selected gravity.

If you are new to Alert Dialog then check its tutorial here.

7. Now, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

Download Source Code – >

Leave a Comment

Comment (required)

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=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)