Home > Net >  Android Studio - Same onClick function for multiple views
Android Studio - Same onClick function for multiple views

Time:08-18

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!

CodePudding user response:

You can set the same on click listener to multiple views.

val tintChanger = View.OnClickListener { view ->
  println("View with id=${view.id} clicked")
  changeTintOf(view as ImageView)
}

imageViewOne.setOnClickListener(tintChanger)
imageViewTwo.setOnClickListener(tintChanger)
imageViewThree.setOnClickListener(tintChanger)

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 argument view: View.

// MainActivity.kt

fun changeTintOnClick(view: View) {
  println("View click listener set by XML")
  println("View clickView with id=${view.id} clicked")
  
  changeTintOf(view as ImageView)
}

private fun changeTintOf(view: ImageView) {
  // your implementation for tint
}
<ImageView ...
  android:onClick="changeTintOnClick" 
    />

CodePudding user response:

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.

class MainActivity : AppCompatActivity(), View.OnClickListener{ /*Extend class to conformalso to View.OnClickListsner*/

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main) 
    }

    //Let's say you have two or more views to use.
    //You want to include the same listener to all opf the same buttons you want to use
    findViewById<Button>(R.id.myelementone).setOnClickListener(this)
    findViewById<Button>(R.id.myelementTwo).setOnClickListener(this)

    override fun onClick(v: View) {
        switch(v.tag){
            case "myelementonetag":
                //Do something
                break;
            case "myelementtwotag"
                //Do something else
                break;
            default:
                //If no tags match the clicked item
                break;
        }
    }
}

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!):

<Button
    android:id="@ id/myelementone"
    tag="myelementonetag"/>
<Button
    android:id="@ id/myelementone"
    tag="myelementonetag"/>

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?

  • Related