Kotlin Android Custom Toast
In the previous example, Kotlin Android Toast we display a simple toast message. We can also able to customize our message which will show as a toast. In the toast, we can display String text, image or both and customize the position of it.
Kotlin Android Custom Toast Example
In this example, we will display text and an image as a custom toast on clicking the Button.
Add the following code in the activity_main.xml file. In this file, we added a Button to perform a click action.
Create a layout file named as custom_toast.xml and add an ImageView and TextView to display as a toast message. Set an image to ImageView and place a message in TextView.
In this MainActivity.kt class, we are getting the View object using the layoutInfleter.infalate() method. Clicking on the button displays the toast message with image and text. The custom layout is added over toast by calling the View using the instance of Toast class (as myToast.view = layout).
The above code performing the following tasks: