I have a button which contains a drawable and text. I want the background of the button to be different than the normal one provided (preferably a plain color). This works f
EXAMPLE:
XML file saved at res/color/button_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
This layout XML will apply the color list to a View:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />
@color/button_text
res/color/button_text.xml
This is the example provided by Google in the ColorStateList Resource: https://developer.android.com/guide/topics/resources/color-list-resource.html
I think colorStateList must be used to change color of textView of a widget.
The xml you posted is suitable for a color state list, not a state list drawable. Try this instead:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" >
<shape><solid android:color="@color/green"/></shape>
</item>
. . .
</selector>
Alternatively, put your existing file into res/color
and use it as you would any other color. However, I don't remember if you can use a color state list directly as a background for a view.