So I’d like to make a function that changes the tint of the view that is calling the function.
I’m pretty sure I got the tint part down I’m just not quite sure how or where I need to define this function so that I can either select it as the onClick method in the built-in properties menu or I can reference it in the xml file(preferably the former).
Right now I have the function in the MainActivity.kt file inside the class and I selected the function on all the different views in the properties menu but when I run the app and actually click on of these views I get a crash saying "Could not find method in parent or ancestor Context for android:onClick attribute"
I would really appreciate some help with this, thanks in advance!
2
Answers
One way you can pull this of is placing an OnClickListener extension on your main activity class and set all of the views with an id and tag to reference later.
The only thing you really need to do in XML is set the id of the element and the tag of the elements like this (using onClick in XML is no longer best practice and advised that it should not be used anymore. Dont forget to keep they styling for you own button!):
If you need a little more on how this works, here is another StackOverflow question that was answered with all best ways to implement click functions: Android – How to achieve setOnClickListener in Kotlin?
You can set the same on click listener to multiple views.
How to set on click listener to views without knowing their ids?
Not preferred way nowadays but if you want to set a click listener on your view by xml, your activity should contain a public method
changeTintOnClick
with an argumentview: View
.