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.

Custom toast Example
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!
More Posts related to Android,
- Change Android Toast background color
- Maven : java.lang.ClassNotFoundException: Xmx512m
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Android Alert Dialog with Checkboxes example
- Android Error Generating Final Archive - Debug Certificate Expired
- How to add Newline to text in Android TextView
- Read Text file from SD Card : Android Programming
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- ActivityManager Warning: Activity not started, its current task has been brought to the front
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Android Developers Bluetooth Tutorial
- java.lang.ClassNotFoundException android.support.v7.widget.Toolbar [Fix]
- Android: Save Data in local Db using Android Room
- Channel 50 SMSes received every few minutes Android Phones
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Changing Android Intent Tittle using java code
- Android : No Launcher activity found! Error
- How to change TextView or EditText Text Color on Focus and on Press
- How to display Toast on Button Click : Android
- Android : Execute some code after back button is pressed
- Stop android adb service from command prompt or terminal
- [Soluiton] You already have the latest version of Android Studio installed
- Create Custom Android AlertDialog
- Android R Cannot Be Resolved To A Variable
- How to make Android EditText not editable
More Posts:
- Java 8: Steam map with Code Examples - Java
- How to extend retiring SharePoint 2010 Workflows and continue with Office 365 - SharePoint
- How to make a div tag clickable - Html
- [Maven] Multiple annotations found at this line pom.xml CoreException, ArtifactResolutionException - Java
- ASCII to HEX and HEX to ASCII Conversion Notepad++ - NotepadPlusPlus
- Facebook Down Will Be Back Soon - Facebook
- How to add ruler in Sublime Text tab window - Sublime-Text
- Detect Data roaming in Android Application - Android