Search code examples
androidxmlonclickonclicklistenerimagebutton

Android ImageButton OnClick calls another function in the same LinearLayout


I have got 3 ImageButton in one LinearLayout and the problem is that, when clicked, "@id/yellow" calls the second button function, "@id/green" calls the third button function and "@id/brown" calls its own function instead (how it is supposed to do).

Here is the XML:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">


            <ImageButton
                android:id="@+id/yellow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleX="4"
                android:scaleY="2.5"
                android:layout_marginLeft="13dp"
                android:layout_weight="1"
                android:onClick="yellowPotted"
                android:background="@drawable/ball_yellow"/>

            <ImageButton
                android:id="@+id/green"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleX="4"
                android:scaleY="2.5"
                android:layout_marginLeft="13dp"
                android:layout_weight="1"
                android:onClick="greenPotted"
                android:background="@drawable/ball_green"/>

            <ImageButton
                android:id="@+id/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleX="4"
                android:scaleY="2.5"
                android:layout_marginLeft="13dp"
                android:layout_weight="1"
                android:onClick="brownPotted"
                android:background="@drawable/ball_brown"/>

        </LinearLayout>

Here are the 3 functions in my activity (I do not know whether they could be useful):

public void yellowPotted(View view) {

    Log.d("BALLS", "YELLOW");

}

public void greenPotted(View view) {

    Log.d("BALLS", "GREEN");

}

public void brownPotted(View view) {

    Log.d("BALLS", "BROWN");

}

I have already tried using setOnClickListener() and override onClick() method in the new View.OnClickerListener() directly in the code of my activity, but it does not work either (exact same problem).

Anybody knows what could be the issue?


Solution

  • I tried reproducing your problem. I think it has to do with the scales that you set. I tried your code out with different scales and different backgrounds, it works. Try it out with different scales for the images.