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,
- Android : Execute some code after back button is pressed
- Android is starting optimizing... app 1 of 1
- How to change Android Button Color using xml attribute and programatically using java
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- How to make Android EditText not editable
- Make Android TextView Clickable like Buttons
- Unable to establish connection to adb : Android Studio Error
- Android Constant and Resource Type Mismatches Lint
- Android Shared Preferences API tutorial
- Fixing Android unknown error 961 while downloading app
- DDMS files not found hprof-conv.exe
- How to reset eclipse layout
- Android Developers Bluetooth Tutorial
- Android Studio Native typeface cannot be made error
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Multiline EditText in Android Example
- How to screenshot on Android?
- JavaScript : redirect page to other url
- Unable to load VM from snapshot. The snapshot has been saved for a different hardware configuration
- How To Disable Landscape Mode in Android Application
- Change Title text for Android Activity using java code
- Android : Class file collision: A resource exists with a different case
- Android Emulator Soft Back button action using Computer keyboard
- Device not compatible error Android Google Play Store
- ERROR x86 emulation currently requires hardware acceleration. Intel HAXM is not installed on this machine
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- Disable Fading Edges Scroll Effect Android Views
- Android : Unable to load VM from snapshot : Mac OS X Error
- Change Android EditText Cursor Height
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- How to add border to Android TextView
- Remove ActionBar from Activity that extends appcompat-v7
- How to send SMS on Android Emulator
- Android RatingBar Example
- This Toast was not created with Toast.makeText() : Android RuntimeException
- Android Studio Ctrl Shift o auto import not working
- Android : Duplicate registration for activity com.example.abc
- ADT Installation Error: requires plug-in org.eclipse.wst.sse.ui
- Running Android Lint has encountered a problem NullPointerException Error
- Android Emulator] ##KBD: Full queue, lose event Error Logs
- Export aborted because fatal lint error were found
- Android Parsing Data for android-L failed Unsupported major.minor version 51.0 Error
- Android : Accidental Octal Lint Warning
- Android ListView turns Black or Flickers while Scrolling
- How to make a dummy phone call from Android Emulator device
More Posts:
- Android : Error in http connection java.net.SocketException: Permission denied - Android
- How to create Toast messages in Android - Android
- How to Search Something (string) in Android Studio Project like Eclipse - Android-Studio
- How to detect Browser and Operating System Name and Version using JavaScript - JavaScript
- Free Unlimited Calls from MTNL & BSNL Landlines from 1st May 2015 - HowTos
- Adding internet permission to Android Project - Android
- How to turn off Facebook autoplay videos on timeline - Facebook
- Android : How to make TextView Scrollable - Android
- Sharepoint Server 2016 installation Prerequisites with download links - SharePoint
- How to get current URL Location using Javascript HTML - JavaScript
- Android Toast position top - Android
- How to submit website to dmoz directory - HowTos
- SharePoint Server 2016 setup error - A system restart from a previous installation or update is pending. Restart your computer and run setup to continue. - SharePoint
- Java XML-RPC 3.1.x based web service example - Java
- Plug-in com.android.ide.eclipse.adt was unable to lead class Error - Android