How to use Toast in Android part 2(end)
Hello everyone, in the section before we already learn about how to use Toast in Android. Now in this section, we will practice the trick and tips to implement the Toast efficiently. Let’s get started on it.
Let say you have one Activity with multiple buttons that you want to make sure all of the buttons can be clicked. In this case, I will make three buttons.
First, you need to add three-button in your layout XML file, you can use Linear Layout, Relative Layout, and Constraint Layout, in this section we will use the Constraint Layout because it very flexible and useful for nested layout design
After that, we need to add some code in MainActivity.kt
And when you run the program, every button you click will give a response like an image below:
The above method is a good way to make sure the buttons are running and responses when clicked, but at that method, we need to declare the Toast multiple times and that is not efficient, how about we just make one Toast and call it multiple times when it necessary to use it? of course, that is will be the efficient way. Let’s make it just one Toast in the MainActivity.kt with these steps:
1. Make the function to declare Toast that will be using multiple times
2. Now you can call the function multiple times whenever you need to use it
Alright, this is the last session for Toast, I hope you enjoy it. If there is a thing you don’t understand or something is confusing in my explanation feel free to ask in the comment box. I will be happy to share with you.