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!
More Posts related to Android,
- Increase Android Emulator Timeout time
- Android : Remove ListView Separator/divider programmatically or using xml property
- Error : Invalid key hash.The key hash does not match any stored key hashes
- How to Change Android Toast Position?
- Android Alert Dialog with Checkboxes example
- Android : No Launcher activity found! Error
- Android Development: Spinners with Example
- Failed to sync Gradle project Error:failed to find target android-23
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Disable Fading Edges Scroll Effect Android Views
- How to create Toast messages in Android?
- Channel 50 SMSes received every few minutes Android Phones
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Create Custom Android AlertDialog
- How To Disable Landscape Mode in Android Application
- Android Development - How to switch between two Activities
- incorrect line ending: found carriage return (\r) without corresponding newline (\n)
- Generate Facebook Android SDK keyhash using java code
- Android Error Generating Final Archive - Debug Certificate Expired
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Android RatingBar Example
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020
- Download interrupted: Unknown Host dl-ssl.google.com Error Android SDK Manager
- fill_parent vs match_parent vs wrap_content
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect
More Posts:
- Fix Generics: error unexpected type required: class found: type parameter - Java
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0 - Java
- How to add a Task List to Quick View Menu in SharePoint Online Site - SharePoint
- Install GitHub Command Line Tool on Mac - Git
- How to Write Code in Windows Notepad - Windows
- Working with Bluetooth on Mac Terminal using blueutil Commands - MacOS
- How to change Android EditText Cursor Color - Android
- Ubuntu: How to set Environment Variable - Ubuntu