I have made a custom component like Mybutton.java and I have set an onclick listener in Mybutton.java.
Now, in my new activity, I have to call a Mybutton and add content in onclick listener.
However, if I use OnClickListener mClickListener = new OnClickListener(){......
it will replace the old content.
I hope it can do the old and new listener together.
I have searched for some information, found out i can implement this method. After many attempts, I'm still getting errors.
Can anyone give me a simple example that i can learn to modify it?
I don't think there's an API in the Android API that allows multiple
onClick
listeners. You'd need some custom class that handles a singleonClick()
and pass in handlers for it to call. Something like this:When your setting your buttons, do:
Then, whenever you want to add another listener, just call
If you want to execute some internal logic in your custom view's onClick and want to execute the externally set up OnClickListener's logic, I think a simple way is overriding setOnClickListener as below.
In Kotlin:
Same in Java:
You could create your custom
Button
class something like this :and, use it like this :