Reputation: 2574
That I want to do is change the color of the button if is press by the user. I need to states red/green. Button start be red, if you press change green and if you press again comeback to red. If press again, green....
I'm trying to find something similar but I can't find anything. Is possible to do it with a drawable selector?
This is that I'm trying and don't remain the color when finish the press
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- green state -->
<item
android:drawable="@drawable/btn_selector_green"
android:state_selected="true"></item>
<!-- green state -->
<item
android:drawable="@drawable/btn_selector_green"
android:state_pressed="true"></item>
<!-- red state -->
<item
android:drawable="@drawable/btn_selecto_come_back"></item>
</selector>
Thanks in advance
Upvotes: 1
Views: 555
Reputation: 41
why you dont do that programaticaly?
just use a simple selector inside of the onClickListener
if(click=1)
button.setBackgroundResource(R.drawable.green);
else
button.setBackgroundResource(R.drawable.red);
Upvotes: 0
Reputation: 983
When the button is pressed its state changes to "state_pressed". However this does not happen for state_selected, if you want to use a Button and the selected state, you have to explicitly set the Button's state to selected via code, add an onClickListener to your button and inside onClick() do:
button.setSelected(!button.isSelected());
This will toggle the selected state every time the Button is pressed.
Upvotes: 1