android toast example

Have you ever seen “touch again to exit” or any other white text in a black capsule shape as pop up in Android???
Those popup messages are toast. In this post, you will learn about it with a simple Android toast example.

Okay… what is toast in Android???

Toast is the subclass of java.lang.Object class, and it helps developers to make notification message that appears at the bottom of the screen by default. The temporary message pops up and fades away after a short time. Just like hot bread pop up from a toaster. While showing toast you can still access your activity.



Okay…Let’s make a toast in android

How to make toast in Android?

Android Toast Example

You can make toast in 2 simple steps.

    1. Call static method  makeText() of Toast class and give
      •  Context
      • CharSequence or String resource id
      • Int duration : LENGTH_LONG or LENGTH_SHORT
    2. Call show() method


Toast toast=Toast.makeText(getApplicationContext(),"Yeah!... I am Toast",Toast.LENGTH_LONG);
toast.show();

Make a toast in one statement.



Toast.makeText(getApplicationContext,"Yeah!... I am Toast",Toast.LENGTH_LONG).show();

Don’t forget to add the show() method, because it happens sometimes for me and thinks why the code doesn’t work after a long stare at the bottom of the screen.

You can make toast in the background services. if your application not in the foreground while toast showing up,  Toast may confuse the users. So use appropriate text with the toast to assist users to digest easily.

What happens if there is no text ??

what happens if the code like below??



Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT).show();

then the output will be as shown below.android toast example

When you call makeText with your parameter value. Internally, it inflates $YOUR_SDK$/platforms/android-22/data/res/layout/transient_notification.xml and set your content to textview which have id message.

android-22 is API level, It may change based on your SDK.

How to change the position of Toast?

Just told you earlier, Toast pop up at bottom of the screen, center-horizontally. Toast class provides

setGravity(int gravity_constant,int xOffsetValue, int yOffsetValue)

method to position your toast where you want.

  1. int gravity_constant: Android provides lots of Gravity constant values. Just like CENTER, TOP, RIGHT, BOTTOM and so on.
  2. int xOffsetValue : If you want to change the position of your toast to the right, increase this value.
  3. int yOffsetValue : If you want to change the position of your toast to downward, increase this value.

Below code will place your toast to center of the screen.


toast.setGravity(Gravity.CENTER,0,0);

you can use more gravity constants using the vertical line (|) symbol.

Below code places your toast to CENTER-RIGHT end.



toast.setGravity(Gravity.CENTER | Gravity.Right,0.0);

More about Toast class

static constants

LENGTH_LONG: It shows view or toast for almost 3.5 seconds.

LENGTH_SHORT: It shows view or toast for almost 2 seconds.

Public constructor

Toast(Context context)

It creates a Toast object.

Public methods

i) public void cancel()

It hides the toast if it is already showing or if it isn’t visible yet.

ii) public int getDuration()

It returns the duration of view or toast. (LENGTH_LONG or LENGTH_SHORT)

iii) public void getGravity()

It returns location if that is already set.

iv) public float getHorizontalMargin()

it returns the horizontal margin in float value.

v) public float getVerticalMargin()

it returns the vertical margin in float value.

vi) public View getView()

It returns the view. You can access view items using this method.

vii) public int getXOffset() && public int getYOffset()

It returns X offset and Y offset in Pixels.

viii) public void setDuration(int duration)

Use this method to set the duration of your toast or view.

ix) public void setMargin(float horizontal_margin, float vertical_margin)

Use this method to set margins of your view or toast. actually here margin means how much space needed between toast and side of the screen.

x) public void setText(int Resource_id or CharSequence string)

There are two variations of setText(…) method. One receives parameter as charSequence and other receives as string resource id. You can update the text in the toast that previously you create with makeText() method.

xi) public void setView(View view)

It sets the view for toast. It’s helpful while creating a custom view.

xii) public void show()

It shows the toast or view.

Create a project – Android Toast Example

In this project, we will make a button inside LinearLayout. When the button is tapped, a toast will appear at the center of the screen with application name in it.

File->New->New Project

Application name: Toast Ex

company domain: androidride.com

click Next and select your minimum SDK and empty activity template.

Activity name: MainActivity

Layout name: activity_main.xml

click Finish.

open activity_main.xml, and paste below code.
Linearlayout gravity attribute place button at the top position, center-horizontally. android:onClick attribute calls showToast() method in MainActivity. Then the toast appears.



<!--?xml version="1.0" encoding="utf-8"?-->


    <button></button>



open MainActivity.java and paste below code



package com.androidride.toastex;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

  public void showToast(View view)
  {
        Toast toast=Toast.makeText(this,R.string.app_name, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
  }


}

Here, makeText() method returns a toast object and Using toast instance setGravity() method position toast at the center of the screen and show() method helps to display the toast.

Run now

android toast example - how to use toast in android - androidride.com

Don’t worry if your ActionBar’s color is blue. You can change it through by copy-pasting my colors.xml given in full code.

For more information
Visit Toast documentation

Download Android Toast Example

Sharing is caring…..

Please disable your adblocker or whitelist this site!