How To Show Custom Toast In Android?

When it's comes to display light message in response to options performed by app Android platform offers Toast.Toast is a light messages display near the bottom,horizontally centered to the screen.Toast can display maximum two line of message.Toast messages are not clickable and hide automatically after sometime.Toast messages are useful when you don't want to block user interface and want to display messages.
Figure 1. shows "Sending message..." text message,indicating message  has been sending.

Fegure 1. Toast Message. 

Show Toast.

To show Toast you need to first initialize Toast object with one of makeText() methods.makeText() method takes three parameters : the application Context, the message to display and duration for the Toast.this method returns fully initialized Toast object.you can show Toast with show() method.

Following code snipped shows how to show simple toast message.

public fun showToast(){
val message = "I am toast message"
val duration = Toast.LENGTH_LONG // or Toast.LENGTH_SORT
val toast = Toast.makeText(applicationContext,message,duration)
//show toast
toast.show()
}
view raw MainActivity.kt hosted with ❤ by GitHub

How To Change Toast Default Position?

Toast by default display near the bottom of the screen,horizontally centered.You can change toast default position with setGravity(int, int, int) method,which takes three parameters: a Gravity constant , a X-position offset and Y-position offset.
Following code snippet shows how to position toast to top right corner of the screen.

public fun showToast(){
val message = "I am toast message"
val duration = Toast.LENGTH_LONG // or Toast.LENGTH_SORT
val toast = Toast.makeText(applicationContext,message,duration)
//set toast postion to top right corner of the screen
toast.setGravity(Gravity.TOP or Gravity.RIGHT,0,0)
//show toast
toast.show()
}
view raw MainActivity.kt hosted with ❤ by GitHub
If you want to change position of x axis of toast then increase or decrease value of second parameter and if you want to change position of Y-axis of toast then increase of decrease value of third parameter.


How To Display Custom Toast?

When simple toast layout can not fulfill your requirements you can create custom view for toast messages.To create custom toast layout define View layout,in XML file or in you application code and pass the root View object to setView(view) method of the Toast object to set this layout.

Following code snipped shows custom layout for the toast message.
Note : save this layout file as custom_toast.xml

<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="#000"
>
<ImageView android:src="@drawable/test"
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>
You can see that ID of the LinearLayout element is "custom_toast_container". You must use this ID and ID of XML layout file to inflate the layout.

Following code snippet shows how to inflate and set custom layout for Toast.

public fun showCustomToast(){
val inflater = layoutInflater
val container: ViewGroup = findViewById(R.id.custom_toast_container)
val customLayout: ViewGroup = inflater.inflate(R.layout.custom_toast, container)
val textView: TextView = layout.findViewById(R.id.text)
textView.text = "Custome Toast Message"
with (Toast(applicationContext)) {
setGravity(Gravity.TOP or Gravity.LEFT, 0, 0)
duration = Toast.LENTH_SHORT
view = customLayout
show()
}
}
view raw MainActivity.kt hosted with ❤ by GitHub
First retrieve LayoutInflater object with getLayoutInflater() or from getSystemService()  method.then inflate your custom layout with inflate(int, ViewGroup) method, which take to parameters first is the ID of layout file you want to inflate and second is root View.You can use inflated View to find more View objects in the layout.So lets find and set content for TextView.At last create the Toast object with Toast(Context) and set properties like duration,gravity Then call setView(View) and pass it the inflated custom layout.You can now display toast by calling show() method.

No comments:

Post a Comment