Introduction to Android Toasts

Android Toast class is a handly feature for showing a small info box to the user. Toast messages are small and they’re usually visible for only a small amount of time. Toast messages are ideal for showing acknowledgements for user actions and other small nice to know infos.

Toasts are pretty easy to create. You initialize the class with the constructor or with the makeText builder method and then class show() when you’re ready to show it.

The following Activity shows how to use the different aspects of Toasts.

Resources

res/values/strings.xml

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

<string name="app_name">Toast Tutorial</string>
<string name="short_button_text">Short Toast</string>
<string name="long_button_text">Long Toast</string>
<string name="gravity_button_text">Gravity Toast</string>
<string name="gravity_toast_text">Custom Gravity!</string>
<string name="custom_button_text">Custom Toast</string>
<string name="custom_toast_text">Toast with custom contents!</string>

</resources>

res/layout/activity_toast.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    tools:context=".ToastActivity" >

    <Button
        android:id="@+id/shortButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:onClick="onShortClick"
        android:text="@string/short_button_text" />

    <Button
        android:id="@+id/longButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/shortButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:onClick="onLongClick"
        android:text="@string/long_button_text" />

    <Button
        android:id="@+id/gravityButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/longButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:onClick="onGravityClick"
        android:text="@string/gravity_button_text" />

    <Button
        android:id="@+id/customButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/gravityButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:onClick="onCustomClick"
        android:text="@string/custom_button_text" />

</RelativeLayout>

res/layout/custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="match_parent"
    android:background="@android:color/white"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center"
        android:text="@string/custom_toast_text" />

</LinearLayout>

Code

src/com/caphal/android/toast/ToastActivity.java

package com.caphal.android.toast;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class ToastActivity extends Activity {

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

  /**
  * Shows a toast for a short time period
  *
  * @param view
  */
  public void onShortClick(View view) {
    Toast.makeText(this, "Short toast", Toast.LENGTH_SHORT).show();
  }

  /**
  * Shows a toast for a longer time period
  *
  * @param view
  */
  public void onLongClick(View view) {
    Toast.makeText(this, "Long toast", Toast.LENGTH_LONG).show();
  }

  /**
  * Shows a toast with a custom gravity
  *
  * @param view
  */
  public void onGravityClick(View view) {
    Toast toast = Toast.makeText(this, R.string.gravity_toast_text, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 0, 0);
    toast.show();
  }

  /**
  * Shows toast with a custom view as a contents
  *
  * @param view
  */
  public void onCustomClick(View view) {

    // Inflate custom toast view
    LayoutInflater inflater = getLayoutInflater();
    View toastLayout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout_root));

    Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(toastLayout);
    toast.show();
  }
}

Screenshots

Basic Toast Toast with a custom gravity Custom content Toast

Source code

Source code for this example project is available here

Further reading

Comments

comments powered by Disqus