Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know what is the button id that was clicked - android

Tags:

android

I have created 3 buttons in my application which when clicked, will go to the method giveClue.

<ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="24dp"
        android:layout_height="22dp"
        android:layout_alignTop="@+id/lifeButtonsLbl"
        android:layout_toLeftOf="@+id/ImageButton2"
        android:src="@drawable/icon"
        android:onClick="giveClue" />

    <ImageButton
        android:id="@+id/ImageButton2"
        android:layout_width="24dp"
        android:layout_height="22dp"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_toLeftOf="@+id/ImageButton3"
        android:src="@drawable/icon" 
        android:onClick="giveClue"/>

    <ImageButton
        android:id="@+id/ImageButton3"
        android:layout_width="24dp"
        android:layout_height="22dp"
        android:layout_alignRight="@+id/frameLayout1"
        android:layout_alignTop="@+id/ImageButton2"
        android:src="@drawable/icon"
        android:onClick="giveClue" />

NOTE: I don't want to use different method for the

ACTIVITY CLASS:

public void giveClue(View view) {
        gameAdapter.giveClue(game);
    }

My problem is that I want to disable the button that was clicked.I don't want to use different method for each button. Is it possible to determine which button was clicked so I can disable it.

Thank you.

like image 657
newbie Avatar asked Dec 04 '25 14:12

newbie


1 Answers

Check for view.getId() in the giveClue method and compare it to R.id.imageButton1 etc. Normally done with a switch-case :)

like image 147
gosr Avatar answered Dec 07 '25 02:12

gosr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!