Reputation: 1108
I've got some buttons that I want to change background colors upon clicking. As a quiz app, they'll turn red if the wrong one is clicked and green if the right one is clicked. I also wanted to reset the buttons back to default when the question is switched to repeat the process of selecting an answer.
My problem is when I went to implement:
buttonOne.setBackgroundResource(android.R.drawable.btn_default);
buttonTwo.setBackgroundResource(android.R.drawable.btn_default);
buttonThree.setBackgroundResource(android.R.drawable.btn_default);
buttonFour.setBackgroundResource(android.R.drawable.btn_default);
Which is supposed to reset the buttons back to default (right?). Now, when I click the buttons and hold them down the background is orange, and I have no idea why. Anyone know why this is?
I haven't set anything to orange so it seems quite random.
Upvotes: 2
Views: 2069
Reputation: 98
First, you need to create an xml file. For example:
<color name="orange">#FF9900</color>
<color name="green">#009900</color>
<color name="blue">#000099</color>
<color name="red">#bf360c</color>
Now, you can use the colors.
buttonOne.setBackgroundColor(getResources().getColor(R.color.green));
Upvotes: 0
Reputation: 1756
It's possible that the resource you're using is a set of drawables which has an "active" one set, like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bar_bottom_home_active" android:state_pressed="true"/>
<item android:drawable="@drawable/bar_bottom_home"/>
</selector>
That would explain different colour when you hold down the button.
Upvotes: 1
Reputation: 483
Try putting it this way:
buttonOne.setBackgroundColor(YOUR_COLOR_HERE);
In the color it's prefered to add it to your colors' xml file and refer to it
Upvotes: 0