set Selector for Button Programmatically issues



I have a row of buttons and i am setting their selectors for background and text programatically.


background_selector.xml



<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://ift.tt/nIICcg">

<item android:drawable="@android:color/white" android:state_pressed="true"/>
<item android:drawable="@color/black_500"/>

</selector>


color_selector.xml



<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://ift.tt/nIICcg">

<item android:state_pressed="true" android:color="@color/black_500"/>
<item android:color="@android:color/white"/>

</selector>


and i am setting these selector for button as follows:



private void setButtonBackgrounds(Drawable buttonDrawable) {
int sdk = android.os.Build.VERSION.SDK_INT;

if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
btnT.setBackgroundDrawable(buttonDrawable);
} else {
btnT.setBackground(buttonDrawable);
}
}


button's xml:



<Button
android:id="@+id/btnT"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/button_t"
android:textSize="22sp" />


This is the same for all the buttons in the row. The problem is When i click on "T" button, the adjacent button is also changing its state. Please look at the image below.


enter image description here


Why is this happening? I am not sure why this is happening. Can someone help me with this.


No comments:

Post a Comment