A Toast is a short alert message shown on the Android screen for a short interval of time. Android Toast is a short popup notification that is used to display information when we perform any operation in the app. In this article, let’s learn how to create a custom toast in Android using Kotlin.
Note: To create custom styled toast in Android using Java please refer to How to add a custom styled Toast in Android.
Table of Attributes
|LayoutInflater||Instantiates a layout XML file into its corresponding View objects|
|inflate||Inflate a new view hierarchy from the specified XML resource.|
|setGravity||Used to change the position of Toast|
Step 1: Create the Toast Layout
Go to res -> layout (right-click) -> new -> Layout Resource file -> Create (custom_toast_layout.xml) file. Add a CardView to contain the custom toast message and also add a TextView to display the text inside the custom toast message. FrameLayout is used to specify the position of multiple views placed on the top of each other to represent a single view screen.
Step 2: Create a New Kotlin File
Now creates a new Kotlin file and named it as WrapToast.kt to make the code reusable. Go to Project Package (right-click) -> new -> Kotlin file/class -> Create (WrapToast.kt) file. Now we are going to extend the Toast::class with showCustomToast() which will take String and Context as a parameter.
- Inflate the previously created layout (custom_toast_layout.xml) using the layoutInflater.
- After, inflated the layout, find its view. In this case, set the text of the TextView of the message.
- The last step is to create a new instance about the Toast:: class. Then, using its application extension function sets the gravity, the duration, and the layout. Inside apply, call the show() method as well.
Step 3: Create a Button to Show Toast in an Activity
Add a Button inside the ConstraintLayout. So when the user clicks on the button then the custom Toast is popped up on the screen.
Step 4: Create the Toast
After, creating the button to show a toast apply an onClickListener() and pass the Toast message and the context of the activity.
Run as Emulator
- How to add a custom styled Toast in Android
- Android Toast in Kotlin
- Android | What is Toast and How to use it with Examples
- Android | How to Change Toast font?
- How to Add RangeSeekbar in Android Using Kotlin?
- How to add Custom Spinner in android?
- How to add Custom Fonts in Android
- Kotlin | Class Properties and Custom Accessors
- Android | How to add Radio Buttons in an Android Application?
- How to create project in Android Studio using Kotlin
- How to send message on WhatsApp in Android using Kotlin
- Running User Interface Thread in Android using Kotlin
- How to create a custom AlertDialog in Android
- How to add active class on click event in custom list group in Bootstrap 4 ?
- Why Kotlin will replace Java for Android App Development
- Android EditText in Kotlin
- Android LinearLayout in Kotlin
- Android RelativeLayout in Kotlin
- Android ListView in Kotlin
- Android FrameLayout in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.