android - state_pressed event is setting background on wrong button while click -
i use buttons inside linear layout, table row , table layout, maybe has influence.
the problem state_pressed
doesn't work properly. when i'm pressing button a/b/c/d, android selecting button d (it darker).
you can see on image, shadow works properly, background setting on wrong button d (bottom right corner).
after clicking button change background programmatically , set backgrounds red or green. works properly.
the red button selecting, because our opponent selects answer. it's setting programmatically , don't have influence bug.
here 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>
i using static function 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 function make button 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 change
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. time.
Comments
Post a Comment