Skip to content
Advertisement

Function “onButtonClicked” is never use

I have created a function called “onButtonClicked” and I want the button (bt301_tw) to send me to the activity (MainActivity_04_ReadioGroup).

The problem is when I create the function onButtonClicked, I can’t use it and I don’t want to use a setOnClickListener because I’m practicing with different views

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) 
       }
}

enter image description here

Advertisement

Answer

This Works for me:

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

If you are using data binding then you can directly mention view name:

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

Hope this helps

Just to add view.id == R.id.bt301_tw This will never be same.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement