Toast messages in Android Programming are used to display Notification messages on Activity Screen as a popup. This message is displayed as Text that fades-in when displayed and after the duration that has been set is over fades-out.
If you want to learn more about the basics of toast messages in android refer: Toast Android Introduction
By default a Toast message is displayed only text and that too at the bottom of the Activity screen and normally in a gray background with border-radius.

We can customize the Toast message by changing its default implementation defining your own toast XML file.
Custom Toast Example:
Let's create a custom Toast message with an image and 2 TextViews, custom_toast_android.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_custom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88FFFFCC"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sub text for toast message"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout> </LinearLayout>
MainActivity.java
package com.example.customtoastexample;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Default toast Example
Toast toast = Toast.makeText(this,
"Hello, this is a android toast message!", Toast.LENGTH_LONG);
toast.show();
//1. Create an object of LayoutInflater Class
LayoutInflater layoutInflater = getLayoutInflater();
//2. Inflate the layoutInflater
View layout = layoutInflater.inflate(R.layout.custom_toast_android,
(ViewGroup) findViewById(R.id.toast_custom));
//Map both the textviews defined
TextView toastHeader = (TextView) layout.findViewById(R.id.textView1);
TextView toastContextText = (TextView) layout.findViewById(R.id.textView2);
//Set text to both the textviews
toastHeader.setText("Custom Toast");
toastContextText.setText("This is an example of custom toast.");
//Create toast object using getApplicationContext()
Toast customToast = new Toast(getApplicationContext());
//Set duration for the toast
customToast.setDuration(Toast.LENGTH_LONG);
customToast.setView(layout);
//Display the toast
customToast.show();
}
}
Comments:
- This is a good example. This lets me create a toast based on the theme of my App - Thank you!
anon 10 Aug 2020 10:08:43 GMT
- Further comments disabled!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!