EliaszKubala EliaszKubala - 2 months ago 19
Android Question

state_pressed event is setting background on wrong button while click

I use Buttons inside Linear Layout, Table Row and Table Layout, maybe this has some influence.

The problem is that

state_pressed
doesn't work properly. When I'm pressing Button A/B/C/D, Android is selecting Button D (it is slightly darker).

You can see on the image, that shadow works properly, but the background is setting always on the wrong button D (bottom right corner).

After clicking any button I change its background programmatically and set backgrounds to red or green. It works properly.

The red button is selecting, because our opponent selects this answer. It's setting programmatically and don't have any influence for my bug.

Here is my layout code

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rozgrywka"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/icon2"
android:background="#ffffffff"
android:padding="5dp"
android:weightSum="1">

<TableRow
android:layout_width="fill_parent"
android:layout_weight="0.5">

<LinearLayout
android:id="@+id/first"
android:layout_height="fill_parent"
android:layout_weight="1">

<Button
android:id="@+id/buttonA"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_weight=".5"
android:background="@drawable/rozgrywkabutton"
android:gravity="center_vertical|center_horizontal"
android:onClick="odpA"
android:text="A"
android:textAlignment="center"
android:textColor="#ff141414"
android:textSize="14dp" />

<Button
android:id="@+id/buttonC"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_weight=".5"
android:background="@drawable/rozgrywkabutton"
android:gravity="center_vertical|center_horizontal"
android:onClick="odpC"
android:text="C"
android:textAlignment="center"
android:textColor="#ff141414"
android:textSize="14dp" />

</LinearLayout>
</TableRow>

<TableRow
android:layout_weight="0.5">

<LinearLayout
android:layout_height="fill_parent"
android:layout_alignStart="@id/first"
android:layout_weight="1"
android:id="@+id/second">


<Button
android:id="@+id/buttonB"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_weight=".5"
android:background="@drawable/rozgrywkabutton"
android:gravity="center_vertical|center_horizontal"
android:onClick="odpB"
android:text="B"
android:textAlignment="center"
android:textColor="#ff141414"
android:textSize="14dp" />

<Button
android:id="@+id/buttonD"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_weight=".5"
android:background="@drawable/rozgrywkabutton"
android:gravity="center_vertical|center_horizontal"
android:onClick="odpD"
android:text="D"
android:textAlignment="center"
android:textColor="#ff141414"
android:textSize="14dp" />

</LinearLayout>

</TableRow>

</TableLayout>


drawable rozgrywkabutton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="#c7c7c7" />
<gradient android:angle="-90" android:startColor="#d3ced3" android:endColor="#b6b2b6" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="#c7c7c7" />
<gradient android:angle="-90" android:startColor="#e4e4e4" android:endColor="#cbc7cb" />
</shape>
</item>
</selector>


here is example

Answer Source

I was using static function to make button default.

public static void setBacground(Button buttonA, Drawable draw) {
    int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        buttonA.setBackgroundDrawable( draw);
    } else {
        buttonA.setBackground( draw);
    }
}

and in my function which make my button look default

Drawable defualt = getResources().getDrawable(R.drawable.rozgrywkabutton);
Global.setBacground(ButtonA, defualt);
Global.setBacground(ButtonB, defualt);
Global.setBacground(ButtonC, defualt);
Global.setBacground(ButtonD, defualt);

when i change this to

Global.setBacground(ButtonA, getResources().getDrawable(R.drawable.rozgrywkabutton));
Global.setBacground(ButtonB, getResources().getDrawable(R.drawable.rozgrywkabutton));
Global.setBacground(ButtonC, getResources().getDrawable(R.drawable.rozgrywkabutton));
Global.setBacground(ButtonD, getResources().getDrawable(R.drawable.rozgrywkabutton));

everything start work properly. Thanks for your time.