Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
In Android, a Toast provides a simple mechanism to show a brief, non-intrusive message to the user. It appears above all other UI elements for a short time, then fades away automatically. The primary purpose of Toast is to give feedback or information to the user.
Android provides two default durations for a Toast: Toast.LENGTH_SHORT
and Toast.LENGTH_LONG
.
Toast.makeText(context, "This is a short Toast!", Toast.LENGTH_SHORT).show() Toast.makeText(context, "This is a long Toast!", Toast.LENGTH_LONG).show()
By default, a Toast appears near the bottom of the screen, centered horizontally. However, you can change its position using the setGravity
method:
val toast = Toast.makeText(context, "Toast at the top!", Toast.LENGTH_SHORT) toast.setGravity(Gravity.TOP, 0, 0) toast.show()
You can customize the appearance of a Toast by setting your own layout.
First, create an XML layout for the Toast:
res/layout/custom_toast.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#55000000" android:padding="8dp" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_notification" android:layout_marginEnd="8dp"/> <TextView android:id="@+id/customToastText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Custom Toast" android:textColor="#FFFFFF"/> </LinearLayout>
Then, in your Kotlin code:
val layoutInflater = LayoutInflater.from(context) val customToastRoot = layoutInflater.inflate(R.layout.custom_toast, null) val customToastText: TextView = customToastRoot.findViewById(R.id.customToastText) customToastText.text = "Your custom message here!" val customToast = Toast(context) customToast.view = customToastRoot customToast.duration = Toast.LENGTH_LONG customToast.show()
Snackbar
, which is another feedback mechanism provided by Android, or a custom dialog/notification.Introduction to Toast in Android:
Toast.makeText(getApplicationContext(), "Hello, Toast!", Toast.LENGTH_SHORT).show();
Using Toast messages in Android explained:
Toast.makeText(getApplicationContext(), "Message to display", Toast.LENGTH_SHORT).show();
Toast.makeText() method in Android with examples:
Toast.makeText()
is a method to create and display a toast. It requires the application context, message to display, and duration (either Toast.LENGTH_SHORT
or Toast.LENGTH_LONG
).Toast.makeText(getApplicationContext(), "Short Duration Toast", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "Long Duration Toast", Toast.LENGTH_LONG).show();
Different types of Toast messages in Android:
LayoutInflater
for more complex messages.Toast toast = Toast.makeText(getApplicationContext(), "Custom Toast", Toast.LENGTH_SHORT); View toastView = toast.getView(); // Customize toastView as needed toast.show();
Duration of Toast messages in Android:
Toast.LENGTH_SHORT
typically lasts around 2 seconds, while Toast.LENGTH_LONG
lasts around 3.5 seconds.Toast.makeText(getApplicationContext(), "Short Duration Toast", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "Long Duration Toast", Toast.LENGTH_LONG).show();
Positioning Toast messages on the screen in Android:
setGravity()
method to specify the location on the screen. Common positions include Gravity.TOP
, Gravity.BOTTOM
, and Gravity.CENTER
.Toast toast = Toast.makeText(getApplicationContext(), "Custom Position Toast", Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP | Gravity.START, 0, 0); // Adjust position as needed toast.show();
Handling click events on Toast in Android examples:
setView()
method and attach an OnClickListener
.Toast toast = Toast.makeText(getApplicationContext(), "Clickable Toast", Toast.LENGTH_SHORT); View toastView = toast.getView(); toastView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Handle click event Toast.makeText(getApplicationContext(), "Toast Clicked!", Toast.LENGTH_SHORT).show(); } }); toast.show();