Multiple onClickListener In Kotlin

Multiple onClickListener In Kotlin | Android Studio Example

As you know we use onClickListener in Android apps development to handle the click events of a View. So, in this example we will learn on how to handle Android Multiple onClickListener In Kotlin. We will take four different Buttons but we will perform their click events from a single onClick method. So, lets see how to do this.


Multiple onClickListener In Kotlin:

1- Build a new Android Studio Project. Give it name “MultipleClickListeners”. The name of the project totally depends upon your choice. So, you can name it with anything you want.

2- When your project will build successfully then you will have two files. “MainActivity.kt” file and “activity_main.xml” file. We start by building UI in “activity_main.xml” file.




3- In your “activity_main.xml” file first change the default Layout from Constraint Layout to Relative Layout. We will take four Buttons here. The height and width for all the Buttons should be “wrap_content”. We assign the ids to our buttons and give proper names to them as well. This is what we will be doing.



4- This is all for our “activity_main”.xml file. Our complete “activity_main.xml” file with the default Relative Layout is like this.


Multiple onClickListener In Kotlin:




5- So now come to your “MainActivity.kt” file. We will add an interface View.OnClickListener into our MainActivity.



6- Now you will see a red line error under your MainActivity. This is because you have to implement the members of View.OnClickListener interface.


Red line error


7- To remove this error click on that red error and and it will show “Implement members”. Click OK to implement the members of the interface. Now you will see an override fun onClick like this.



8- So in our onCreate method now we are going to add onClickListener for each button. For this we will apply setOnClickListener on every Button separately and pass (this) as reference.



9- After this we have to show a separate Toast message on the click of each button. We make a private var toast and set its initial value to null.



10- Because, we use a separate method to display our Toast message so we will call that method in our onClick function.  



11- So, now we will apply our myToast() function inside the onClick function. For doing this we use when statement. This when statement is now responsible to show the respective Toast message with respect of the clicked Button. This is how our onClick function will look like.



12- So that’s it for this example and this is how our final “MainActivity.kt” file will look like.



13- So, I hope this tutorial helped you to understand how to use multiple onClickListener in Kotlin. And it also helps you to understand the View.OnClickListener interface. But still if you have any queries to ask then please comment below and share this tutorial 🙂





Multiple onClickListener In Kotlin
Multiple onClickListener. In Kotlin





Multiple onClickListener In Kotlin
Multiple onClickListener In Kotlin





        People Also Read




Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!