Making Toast

  • Written by  Clive

How do you like your Toast?

Toast iconToast messages are a quick way of telling the user something.

They are short pop-up messages that show for a second or two and then fade away.

Android basic Toast 

Here’s a basic Toast. It pops up near the bottom of the screen

Basic Toast

Plain, light Toast

The basic Toast contains a short text message.

Here’s how you create a basic Toast.

Use the makeText() method. It has three parameters:

  • Context – usually the application or activity context
  • Message – the text message to display. Use a CharSequence object or String
  • Duration – the time the pop-up is visible. There are two time options:
    • LENGTH_SHORT – about 2 seconds
    • LENGTH_LONG – about 4 seconds

Here’s the code for our basic Toast:

Android basic Toast

Note the following:

  • getApplicationContext() – gets the context. Most of the time you can use this
  • message – use CharSequence although you can also use a String
  • duration – we’re using LENGTH_SHORT so the Toast will appear for about 2 seconds
  • final – used here because I am showing it from within the button onClick() method

Let’s see the Toast!

Call show() to show the Toast, like this:

Android show() Toast

You can also string everything together like this:

Showing Android Toast

Calling this, will create and show the Toast centred near the bottom of the screen. Notice that I’ve used this as the context.

Positioning the Toast

Where do you want your Toast, upstairs or downstairs?

You can position the Toast anywhere on the screen.

Android Toast positioned near the top of the screen

The Toast positioned near the top of the screen

Moving your Toast around

The default position for the standard Toast is centred near the bottom of the screen.

Use the setGravity() method to change the position of the Toast.

Here’s how I did it:

Positioning Android Toast

Note the following:

  • getApplicationContext() – gets the context
  • LENGTH_SHORT – we want the Toast to display for about 2 seconds
  • setGravity() – sets the position of the Toast on the screen. It has three parameters:
    • Gravity – specific placement of an object in a larger container. We use two constants, TOP and LEFT. This places the Toast in the top, left corner of the screen
    • xOffset – changing this value moves the Toast left and right
    • yOffset – changing this value moves the Toast up and down

Custom Toast

Cut the crusts off my Toast, please

You can design the look of your Toast.

Custom Android Toast

A custom Toast using our own layout

Here’s how.

Create your layout and save it in the res>layout folder as an xml file.

You must give the root view (or container) an ID. We've named it custom_toasty_container.

Android custom Toast

Note the following:

  • LayoutInflater – we need a layout inflater to get the views for our Toast (the image and text)
  • R.layout.toast_layout – this is our layout file which is saved in the res>layout folder. It contains the views that will display in the Toast
  • – this is the ID of the root view or container in our layout file
  • TextView – this is the view that displays the text message
  • setText() – we set the text to display in the Toast. The text is a string in the strings.xml file (inside the res>values folder) identified by R.string.custom_toast_text
  • setGravity() – this sets the position of the Toast (see the discussion above)
  • setDuration()  - we use LENGTH_LONG to display the Toast for about 3 or 4 seconds
  • setView() – this sets the Toast to use our layout
  • show() – shows the Toast

Enjoy your Toast!

This project was created using Android Studio. You can download the project files here  Download icon