This must be a silly question as there are so many related question here in stackoverflow and maybe the answer is simple. But anyway here it is.
I’ve got a button and create a drawable xml for it.
So under my button here is the code:
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="30dip"
android:background="@drawable/button2"
/>
Now under my drawable/button2 i have created a selector in which i am not using an image.
Here is my code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#FF9E35" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="0dp"
android:topLeftRadius="8dp"
android:bottomLeftRadius="0dp"
android:topRightRadius="0dp"
android:bottomRightRadius="8dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#FF9E35"
android:endColor="#992f2f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="0dp"
android:topLeftRadius="8dp"
android:bottomLeftRadius="0dp"
android:topRightRadius="0dp"
android:bottomRightRadius="8dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
</selector>
Now what I am trying to accomplish is that when I clicked that button, it change its background color to what is state_pressed is implying and return to normal if another button is clicked(Most likely a toggle button)
Is there anyway to do it on code behind or in xml selector?
Thanks,
JC
What you can actually do in Java code to accomplish it is :
That will change the background of the button if he’s pressed. When the user release his finger of it, it will change the background again. If you want to change the background only if another button is pressed, don’t write the ACTION_UP case and in the other ACTION_DOWN case of another button, change the background of other button, see this below :
Hope it helps !