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!
- Android Error Unexpected cast to Button: layout tag was FrameLayout
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK
- Parsing Data for android-21 failed unsupported major.minor version 51.0
- Android Studio Ctrl Shift o auto import not working
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Android : How to make TextView Scrollable
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Integrating Android Facebook SDK 3.17.2 Tutorial
- Android R Cannot Be Resolved To A Variable
- Android : Exception raised during rendering: action_bar API 22
- How to take screenshot on Android
- Read Text file from SD Card : Android Programming
- How to make Android EditText not editable
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later.
- The declared package does not match the expected package Eclipse
- Can't Run SDK Manager find_java.bat issue
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- Android Emulator Soft Back button action using Computer keyboard
- Multiline EditText in Android Example
- Use 5G Network on Android Emulator
- Make Android TextView Clickable like Buttons
- How to empty trash in Android Device
- Android : Execute some code after back button is pressed
- Disable Fading Edges Scroll Effect Android Views
- How To Disable Landscape Mode in Android Application
- Error code 0xCAA82EE2: Something went wrong (request timed out) [Microsoft] - Microsoft
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj] - Java
- Customize Praise badge for Microsoft Teams with own images, values, brand - Microsoft
- INSTALL_FAILED_INSUFFICIENT_STORAGE Error Android Emulator - Android
- remove div vertical scroll - Html
- Select Line Number TextEdit on Mac - MacOS
- [fix] fatal: pathspec index.html did not match any files error - Git
- How to extract Java Jar/War/Ear files in Linux - Java