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
- Get the Application Context: The Toast class requires a context, which is typically the activity or application context.
- Create a Toast Instance: Use the
Toast.makeTextmethod to create a new Toast. It requires a context, the text to show, and the duration of the Toast.
- Display the Toast: Call the
show()method on the Toast instance to display it on the screen.
Important Parameters
- Context: Generally
this(activity context) orgetApplicationContext(). - Text: The message to be shown.
- Duration: Either
Toast.LENGTH_SHORTorToast.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:
- Create a Layout XML:Create an XML layout file in your res/layout directory. For instance:
- Inflate the Layout:Inflate the layout in your activity and set it to the Toast.
Changing Position and Gravity
Toasts can be positioned differently on the screen using gravity and offset functions.
- 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
| Feature | Description |
| Basic Toast | Simple, baseline message display. |
| Custom Layout | Use XML layout to style Toast. |
| Change Position and Gravity | Position Toast anywhere on the screen. |
| Context Used | Typical context needed like
this, or getApplicationContext(). |
| Message Duration | Toast.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_SHORTandToast.LENGTH_LONGbased 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.

