Android Custom Toast: A Simple Example

I am new to Android programming. What is a simple example of a custom toast notification on Android?

+105
android toast
Jul 02 2018-12-12T00:
source share
14 answers

Use the custom toast code below. It can help you.

toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:background="#DAAA" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="#FFF" /> </LinearLayout> 

MainActivity.java

 LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello! This is a custom toast!"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); 

And check out the links below for your own toast.

Individual toast with analog clock

YouTube: Create your own toast with a button in Android Studio

+184
Jul 02 '12 at 6:10
source share
β€” -

A toast for showing messages in short intervals; So, as far as I understand, you would like to customize it by adding an image to it and changing the size, color of the message text. If that's all you want to do, then there is no need to create a separate layout and inflate it to an instance of Toast.

The default Toast view contains a TextView for displaying messages on it. So, if we have a link to the resource identifier of this TextView , we can play with it. Below is what you can do to achieve this:

 Toast toast = Toast.makeText(this, "I am custom Toast!", Toast.LENGTH_LONG); View toastView = toast.getView(); // This'll return the default View of the Toast. /* And now you can get the TextView of the default View of the Toast. */ TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message); toastMessage.setTextSize(25); toastMessage.setTextColor(Color.RED); toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_fly, 0, 0, 0); toastMessage.setGravity(Gravity.CENTER); toastMessage.setCompoundDrawablePadding(16); toastView.setBackgroundColor(Color.CYAN); toast.show(); 

In the above code, you can add an image to the TextView via setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) , whatever position you want with respect to the TextView.

Update:

They wrote a builder class to simplify the above goal; Here is the link: https://gist.github.com/TheLittleNaruto/6fc8f6a2b0d0583a240bd78313ba83bc

Check out HowToUse.kt in the link above.

Exit:

Enter image description here

+33
May 23 '15 at 9:32
source share

STEP 1:

First create a layout for the custom toast in res/layout/custom_toast.xml :

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_toast_layout_id" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFF" android:orientation="horizontal" android:padding="5dp" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#000" /> </LinearLayout> 

STEP 2: In the action code, get the above custom view and attach to Toast:

 // Get your custom_toast.xml ayout LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_layout_id)); // set a message TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Button is clicked!"); // Toast... Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); 

See how we create custom Toast in Android for more details:

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

+15
Jul 02 '12 at 6:10
source share

See link here . You find your solution. And try:

Create a custom toast view

If a simple text message is not enough, you can create an individual layout for your toast. To create a custom layout, define the layout of the view in XML or application code and pass the root View object to the setView (View) method.

For example, you can create a breadboard for the toast, visible in the screenshot to the right, with the following XML (saved as toast_layout.xml):

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:background="#DAAA" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /> </LinearLayout> 

Note that the identifier of the LinearLayout element is "toast_layout". You should use this ID to inflate the layout from XML, as shown below:

  LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello! This is a custom toast!"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); 

First retrieve LayoutInflater using getLayoutInflater () (or getSystemService ()) and then inflate the layout from XML using inflate (int, ViewGroup). The first parameter is the layout resource identifier, and the second is the root view. You can use this bloated layout to find more View objects in the layout, so now create and define content for ImageView and TextView elements. Finally, create a new Toast with Toast (Context) and set some toast properties, such as gravity and duration. Then call setView (View) and pass it the bloated layout. Now you can display the toast using a custom layout by calling show ().

Note. Do not use the public constructor for Toast unless you intend to define a layout using setView (View). If you don't have a custom layout, you should use makeText (Context, int, int) to create a Toast.

+4
Jul 02 '12 at 6:11
source share

I think most of the custom xml examples on the web are based on a single source.

The documentation on Android, which, in my opinion, is very outdated. fill_parent should no longer be used. I prefer to use wrap_content in conjunction with xml.9.png. This way you can determine the minimum toastbackground size for the entire size of the provided source.

If more complex toasts are required, a frame or relative layout should be used instead of LL.

toast.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/points_layout" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/background" android:layout_gravity="center" android:gravity="center" > <TextView android:id="@+id/points_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:layout_margin="15dp" android:text="@+string/points_text" android:textColor="@color/Green" /> </LinearLayout> 

background.xml

 <?xml version="1.0" encoding="utf-8"?> <nine-patch xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/background_96" android:dither="true"/> 

background_96 - background_96.9.png.

This is not very well tested and hints are welcome :)

+1
May 05 '13 at 21:31
source share

This is what I used

AllMethodsInOne.java

 public static Toast displayCustomToast(FragmentActivity mAct, String toastText, String toastLength, String succTypeColor) { final Toast toast; if (toastLength.equals("short")) { toast = Toast.makeText(mAct, tText, Toast.LENGTH_SHORT); } else { toast = Toast.makeText(mAct, tText, Toast.LENGTH_LONG); } View tView = toast.getView(); tView.setBackgroundColor(Color.parseColor("#053a4d")); TextView mText = (TextView) tView.findViewById(android.R.id.message); mText.setTypeface(applyFont(mAct)); mText.setShadowLayer(0, 0, 0, 0); tView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { toast.cancel(); } }); tView.invalidate(); if (succTypeColor.equals("red")) { mText.setTextColor(Color.parseColor("#debe33")); tView.setBackground(mAct.getResources().getDrawable(R.drawable.toast_rounded_red)); // this is to show error message } if (succTypeColor.equals("green")) { mText.setTextColor(Color.parseColor("#053a4d")); tView.setBackground(mAct.getResources().getDrawable(R.drawable.toast_rounded_green)); // this is to show success message } return toast; } 

YourFile.java

When calling, simply write below.

 AllMethodsInOne.displayCustomToast(act, "This is custom toast", "long", "red").show(); 
+1
Dec 08 '15 at 9:45
source share

You can download the code here .

Step 1:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/btnCustomToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Custom Toast" /> </RelativeLayout> 

Step 2:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/custom_toast_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/custom_toast_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My custom Toast Example Text" /> </LinearLayout> 

Step 3:

 import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button btnCustomToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnCustomToast= (Button) findViewById(R.id.btnCustomToast); btnCustomToast.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Find custom toast example layout file View layoutValue = LayoutInflater.from(MainActivity.this).inflate(R.layout.android_custom_toast_example, null); // Creating the Toast object Toast toast = new Toast(getApplicationContext()); toast.setDuration(Toast.LENGTH_SHORT); // gravity, xOffset, yOffset toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setView(layoutValue);//setting the view of custom toast layout toast.show(); } }); } } 
+1
Jan 05 '16 at 10:38
source share

Custom layout for toast, custom_toast.xml :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Custom Toast" android:gravity="center" android:id="@+id/custom_toast_text" android:typeface="serif" android:textStyle="bold" /> </LinearLayout> 

And the Java method (just pass the toast message to this method):

 public void toast(String message) { Toast toast = new Toast(context); View view = LayoutInflater.from(context).inflate(R.layout.image_custom, null); TextView textView = (TextView) view.findViewById(R.id.custom_toast_text); textView.setText(message); toast.setView(view); toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.show(); } 
+1
12 Sept. '17 at 4:59 on
source share

Code for MainActivity.java file.

 package com.android_examples.com.toastbackgroundcolorchange; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button BT; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BT = (Button)findViewById(R.id.button1); BT.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast ToastMessage = Toast.makeText(getApplicationContext(),"Change Toast Background color",Toast.LENGTH_SHORT); View toastView = ToastMessage.getView(); toastView.setBackgroundResource(R.layout.toast_background_color); ToastMessage.show(); } }); } } 

Code for the activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.android_examples.com.toastbackgroundcolorchange.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="CLICK HERE TO SHOW TOAST MESSAGE WITH DIFFERENT BACKGROUND COLOR INCLUDING BORDER" /> </RelativeLayout> 

Code for the toast_background_color.xml layout file created in the res-> layout folder.

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <stroke android:width="3dp" android:color="#ffffff" ></stroke> <padding android:left="20dp" android:top="20dp" android:right="20dp" android:bottom="20dp" /> <corners android:radius="10dp" /> <gradient android:startColor="#ff000f" android:endColor="#ff0000" android:angle="-90"/> </shape> 
0
Jun 01 '17 at 9:39 on
source share

// Custom toast class where you can show a custom toast or default toast as you need)

 public class ToastMessage { private Context context; private static ToastMessage instance; /** * @param context */ private ToastMessage(Context context) { this.context = context; } /** * @param context * @return */ public synchronized static ToastMessage getInstance(Context context) { if (instance == null) { instance = new ToastMessage(context); } return instance; } /** * @param message */ public void showLongMessage(String message) { Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } /** * @param message */ public void showSmallMessage(String message) { Toast.makeText(context, message, Toast.LENGTH_LONG).show(); } /** * The Toast displayed via this method will display it for short period of time * * @param message */ public void showLongCustomToast(String message) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); View layout = inflater.inflate(R.layout.layout_custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.ll_toast)); TextView msgTv = (TextView) layout.findViewById(R.id.tv_msg); msgTv.setText(message); Toast toast = new Toast(context); toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); } /** * The toast displayed by this class will display it for long period of time * * @param message */ public void showSmallCustomToast(String message) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); View layout = inflater.inflate(R.layout.layout_custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.ll_toast)); TextView msgTv = (TextView) layout.findViewById(R.id.tv_msg); msgTv.setText(message); Toast toast = new Toast(context); toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); } } 
0
Jan 10 '18 at 11:51
source share

An easy way to set up a toast,

 private void MsgDisplay(String Msg, int Size, int Grav){ Toast toast = Toast.makeText(this, Msg, Toast.LENGTH_LONG); TextView v = (TextView) toast.getView().findViewById(android.R.id.message); v.setTextColor(Color.rgb(241, 196, 15)); v.setTextSize(Size); v.setGravity(Gravity.CENTER); v.setShadowLayer(1.5f, -1, 1, Color.BLACK); if(Grav == 1){ toast.setGravity(Gravity.BOTTOM, 0, 120); }else{ toast.setGravity(Gravity.BOTTOM, 0, 10); } toast.show(); } 
0
May 08 '18 at 5:29
source share

To avoid problems with the incorrect use of the layout_ * parameters, you need to make sure that when inflating the custom layout, the correct ViewGroup is specified as the parent.

Many examples skip zero here, but instead, you can pass the existing Toast ViewGroup as your parent.

 val toast = Toast.makeText(this, "", Toast.LENGTH_LONG) val layout = LayoutInflater.from(this).inflate(R.layout.view_custom_toast, toast.view.parent as? ViewGroup?) toast.view = layout toast.show() 

Here we replace the existing Toast view with our custom view. If you have a link to your β€œlayout” layout, you can update any images / textual representations that it may contain.

This solution also prevents any β€œView not attached to window manager” crashes to use null as the parent.

Also, avoid using ConstraintLayout as a custom layout root, this doesn't seem to work when used inside Toast.

0
Aug 29 '18 at 2:02
source share

For all Kotlin users

You can create an extension as follows:

 fun FragmentActivity.showCustomToast(message : String,color : Int) { val toastView = findViewById<TextView>(R.id.toast_view) toastView.text = message toastView.visibility = View.VISIBLE toastView.setBackgroundColor(color) // create a daemon thread val timer = Timer("schedule", true) // schedule a single event timer.schedule(2000) { runOnUiThread { toastView.visibility = View.GONE } } } 
0
Aug 29 '19 at 11:44
source share
 val inflater = layoutInflater val container: ViewGroup = findViewById(R.id.custom_toast_container) val layout: ViewGroup = inflater.inflate(R.layout.custom_toast, container) val text: TextView = layout.findViewById(R.id.text) text.text = "This is a custom toast" with (Toast(applicationContext)) { setGravity(Gravity.CENTER_VERTICAL, 0, 0) duration = Toast.LENGTH_LONG view = layout show() } <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_toast_container" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8dp" android:background="#DAAA" > <ImageView android:src="@drawable/droid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /> </LinearLayout> 

Link: https://developer.android.com/guide/topics/ui/notifiers/toasts

-one
Sep 05 '18 at 12:33
source share



All Articles