Android Development
Toast Notification
Android UI
Mobile App Development
Android Programming

How to display Toast in Android?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Toasts in Android provide a simple way to show brief messages to the user. They are transient, and appear over the main content of an activity without blocking the interaction with the UI. Toasts are perfect for non-obtrusive, temporary messages such as displaying the status of an action or providing quick feedback to the user. This article explains how to implement and customize Toasts in Android applications.

Basics of Toast

A basic Toast is easy to create in Android. To display a simple message using Toast, follow these steps:

Step-by-Step Implementation

  1. Get the Application Context: The Toast class requires a context, which is typically the activity or application context.
  2. Create a Toast Instance: Use the Toast.makeText method to create a new Toast. It requires a context, the text to show, and the duration of the Toast.
java
    // Display a basic Toast
    Toast toast = Toast.makeText(getApplicationContext(), "Hello, World!", Toast.LENGTH_SHORT);
    toast.show();
  1. Display the Toast: Call the show() method on the Toast instance to display it on the screen.

Important Parameters

  • Context: Generally this (activity context) or getApplicationContext().
  • Text: The message to be shown.
  • Duration: Either Toast.LENGTH_SHORT or Toast.LENGTH_LONG.

Customizing Toast

While the default Toast is helpful, customizing it can enhance user experience and align it with your app's UI.

Custom Layout for Toast

To create a Toast with a custom layout:

  1. Create a Layout XML:
    Create an XML layout file in your res/layout directory. For instance:
xml
1    <!-- res/layout/custom_toast.xml -->
2    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3        android:orientation="horizontal"
4        android:padding="10dp"
5        android:background="#CC000000"
6        android:layout_width="wrap_content"
7        android:layout_height="wrap_content">
8
9        <ImageView
10            android:src="@drawable/ic_info"
11            android:layout_width="wrap_content"
12            android:layout_height="wrap_content"
13            android:layout_marginEnd="8dp"/>
14
15        <TextView
16            android:id="@+id/toast_text"
17            android:textColor="#FFFFFF"
18            android:textSize="16sp"
19            android:layout_width="wrap_content"
20            android:layout_height="wrap_content" />
21    </LinearLayout>
  1. Inflate the Layout:
    Inflate the layout in your activity and set it to the Toast.
java
1    LayoutInflater inflater = getLayoutInflater();
2    View layout = inflater.inflate(R.layout.custom_toast, 
3                                   (ViewGroup) findViewById(R.id.custom_toast_layout));
4
5    TextView text = layout.findViewById(R.id.toast_text);
6    text.setText("Custom Toast Message");
7
8    Toast toast = new Toast(getApplicationContext());
9    toast.setDuration(Toast.LENGTH_SHORT);
10    toast.setView(layout);
11    toast.show();

Changing Position and Gravity

Toasts can be positioned differently on the screen using gravity and offset functions.

java
Toast toast = Toast.makeText(getApplicationContext(), "Positioned Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.show();
  • Gravity: Determine where the Toast will appear on the screen.
  • Offset Values: xOffset and yOffset specify the screen position regarding the horizontal and vertical axis.

Key Points Summary

FeatureDescription
Basic ToastSimple, baseline message display.
Custom LayoutUse XML layout to style Toast.
Change Position and GravityPosition Toast anywhere on the screen.
Context UsedTypical context needed like this, or getApplicationContext().
Message DurationToast.LENGTH_SHORT or Toast.LENGTH_LONG.

Best Practices

  • Do not overuse Toasters: Too many Toaster messages can overwhelm and annoy users, reducing their effectiveness.
  • Clear and concise messages: Be succinct and ensure the message displayed is straightforward and easily understood.
  • Proper Timing: Adjust the timing between Toast.LENGTH_SHORT and Toast.LENGTH_LONG based on the message length.

Conclusion

Toasts are a versatile way to provide quick feedback to users without interrupting the flow of an application. With customization options, they can be both functional and aesthetically aligned with your app’s theme. Implementing Toast correctly can significantly enhance the usability and user experience of Android applications.


Course illustration
Course illustration

All Rights Reserved.