The Toast's Setview (View view) method allows you to set the content of the toast to any view
eg
Toast_layout.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout android:id= "@+id/toast_layout_root" xmlns: Android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android: layout_height= "Match_parent" android:orientation= "vertical" > <imageview android:layout_width = "Wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/cover"/> < TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android: gravity= "center" android:text= "I am a custom toast" android:textsize= "20sp" android:typeface= " Monospace "/></linearlayout>
Mainactivity.java
Package Com.panjn.userinterface;import Android.support.v7.app.appcompatactivity;import Android.os.Bundle;import Android.view.gravity;import Android.view.view;import Android.view.ViewGroup;
Import Android.widget.toast;public class Mainactivity extends Appcompatactivity { @Override protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); View Toastview = Getlayoutinflater (). Inflate (R.layout.toast_layout, (ViewGroup) Findviewbyid (R.id.toast_layout_root )); Toast toast = new Toast (Getapplicationcontext ());// Toast.settext ("Hello Minions"); Cannot call SetText (String text), otherwise throws an exception Toast.setview (Toastview); Toast.setduration (Toast.length_long); Params:int Gravity, int xoffset, int yoffset toast.setgravity (gravity.center,0,0); Toast.show (); }}
User Interface Custom Toast