Programming and Personal Development

Toasts in Android

Toast messages in android are a way to display a info level message such as “Battery low”/”Update successful” to the user. Toasts  however do not try to grab the attention of the user by explicitly asking for acknowledgement, meaning,

Android Toast Demo

Android Toast demo

  • they just silently pop-up without blocking the user’s current activity.
  • display the required message and disappear.
There is no guarantee that the user even noticed the message. Use “alert” messages if you need user to acknowledge the message.
To display a Toast message you can use the makeText() static method which takes :
  • the Context ( usually the activity instance),
  • the message string or  a resource id that refers to the message string, and
  • a duration

as arguments and returns a Toast instance. You can then call the show() method to display the toast. Note that if you forget to call the show() method , the message will not be visible on the screen and  android does not complain either as its not an error.  You can control how long you want to display the toast on the screen by supplying one of the two pre-defined durations, Toast.LENGTH_SHORT

Android xml layout for Toast demo

Main xml layout file

and Toast.LENGTH_LONG.

Custom toasts in android

By default, the toast messages appear as a rectangular box as shown above.  You can customize the

Android XML layout for custom Toast demo

XML layout for Custom Toast View

appearance  by providing your own View as argument to the setView(). Instead of using the makeText() method,

  • create a new instance of the Toast
  • Set the desired view
  • Set the duration
  • Call show.

Toast Example

Lets look at a simple example. In the first xml layout file we have the view for main activity with 2 buttons.  In the second layout file we have a ImageView element and a TextView Element wrapped inside a Linear Layout.  we’ll use this to display the  toast with a custom view as shown below.

Now lets look at the Java code. The useful elements are highlighted for easy reference.

Custom view for toast in android

Android Toast Custom View

Java Code for Android Toast Demo

Java Code for Toast Demo

  1. Inside the onClickListener() for the first button we use the static  makeText() method to create a new instance of Toast  We then set the duration and call show() on the Toast instance.
  2. When we use the setContentView() method with a desired layout, android automatically reads the xml layout file, parses it, creates the necessary UI objects in memory and renders them on the screen. Not only is this expensive, but also a very tedious process to do it manually. Luckily we don’t have to because android exposes the LayoutInflater class that is available to all activities via the getLayoutInflater() method. You can use the inflate() method and pass it a resource id and an optional parent container to inflate the view based on a xml layout resource.
  3. We create an android custom toast message with the inflated view and display it on the screen. We set the duration to Toast.LENGTH_LONG as shown.
Web Analytics