Function “onButtonClicked” is never use

I have created a function called “onButtonClicked” and I don’t want to use a setOnClickListener because I am practicing with different views

the problem is when I create the onButtonClicked function, I see this warning

`(the “onButtonClicked” function is never used)

My code:

XML

 <Button
            android:id="@+id/bt301_tw"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="?android:attr/colorActivatedHighlight"
            android:onClick="onButtonClicked"
            android:text="@string/rodiogroup_301" />
Mainactivity

fun onButtonClicked(view: View) {
        if (view.id == R.id.bt301_tw) {
            val intent = Intent(this, MainActivity_04_ReadioGroup::class.java)
            startActivity(intent) 
       }
}

Hey @jgamarra!
In case of work with different views you can set custom click listener using setOnClickListener() without using XML onClick method

        val button1: Button = findViewById(R.id.navigation_home)
        val button2: Button = findViewById(R.id.navigation_dashboard)
        
        val clickListener: View.OnClickListener = View.OnClickListener { 
            if (it.id == R.id.navigation_home){
                //todo so
            }
            else if (it.id == R.id.navigation_dashboard){
                //todo something new
            }
        }
        
        button1.setOnClickListener(clickListener)
        button2.setOnClickListener(clickListener)
1 Like