Custom Toast,Android(Kotlin).

We are going to learn about Custom Toast in Android,Kotlin.

MandviVerma
2 min readJul 11, 2020

In this article I’m going to tell you how to implement Custom Toast in Android by inflating custom layout.

Step 1:Add the code below in the activity_main.xml

I have defined a button “Show Custom Toast”. When this button is clicked a custom toast will be displayed.

Step 2:I have created a new layout resource file named custom_layout.xml and it contains XML code for custom toast .

(res->layout->new->layout resource file)

In this I have created the custom toast which will be displayed when a button “Show Custom Toast” is clicked.

Add the below code in custom_layout.xml

Step 3:Add the code below in MainActivity.kt

So,in MainActivity.kt we are creating toast object and inflating custom_layout to toast view.

.setGravity() method is used to position the custom toast with respect to the screen.Here I have positioned the custom toast to the center of the screen.

.duration is to specific that for how much time you want to display the toast.

.show() method is used to show the custom toast

OUTPUT:-

When the user clicks the “Show Custom Toast” button created in activity_main.xml, a custom toast will pop up.

Liked it? Have a question?

Hope you liked the guide. In case you have any issues, feel free to contact us or drop a response, We’ll be glad to help you out.

Thank you for going through the article and Please do follow.

--

--