Android Custom Toast
Today we are going to learn how can we make our own custom Toast. Nowadays a lot of apps are using custom toast to show messages to clients. Sometimes it is requirement of design to make a custom toast. Don't worry, it is very simple and easy to create custom toast in android. But the special thing is today we will make the custom toast using kotlin language. Here is the final look of our Toast.Android Custom Toast with Kotlin - Android Tech Point |
Steps
As I have mentioned earlier that it is very easy to make a toast using the Toast as super class. As we are using kotlin, we can do this by using extended functions of kotlin.
- Create custom_toast_layout.xml and define the layout for our custom toast.
- Create a kotlin file and name it Util.kt.
- Write an extended function createToast() to show the toast. You can pass different properties to change the behavior and layout of the toast.
- Use your own custom toast anywhere in the project.
Implementation
Here are the simple steps in detail to create the custom toast.
- Creat the custom_toast_layout.xml.
- Second step is to write the extended function for the Toast class.
- Final step is to use this function. You can access this function from anywhere in your project. Here is how you can use this function.
Final Words
We can customize this function in any way we want according to our requirement. A=That's all. We are done with the custom toast. Happy Coding!! :) You can find the code for the complete project at GitHub. Feel free to discuss anything in the comments or contact me at farooqahamdkhan003@gmail.com.