8

I am trying to very simply style a Button. I just want it blue with with text when not pressed, and white with blue text when clicked.

I tried to do this with a style and a selector.

In my layout I have this Button:

 <Button
    android:id="@+id/button1"
    style="@style/MyButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="@string/login" />

And in res/values/styles I have this styles.xml:

<style name="MyButton">
    <item name="android:background">@drawable/btn_background</item>
    <item name="android:textColor">@drawable/btn_textcolor</item>
</style>

And of course, the two selectors, in res/drawable, btn_background.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="@color/white" />
   <item android:color="@color/SapphireBlue" />
</selector>

and btn_textcolor.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="@color/SapphireBlue" />
    <item android:color="@color/white" />
</selector>

The error I get now when I either run the app, or open the layout editor is:

<item> tag requires a 'drawable' attribute

I understand the message, but I don't have a drawable, is it is a simple, flat button.

How can I create such a simple button?

Update according to this post, it should work.

Community
  • 1
  • 1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Create a basic `shape` drawable with only the `solid` attribute specified, set to your target color. http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape – user Sep 20 '13 at 09:58
  • 2
    @Luksprog ok it works now, but don't we all agree it is **insane** that I have to create 5 XML files just to change a Button's "active" color and background? There must be a better way.... – Bart Friederichs Sep 20 '13 at 10:05
  • Have a look at Dynamo's answer. – user Sep 20 '13 at 11:54

3 Answers3

24

Try this way,hope this will help you to solve your problem.

btn_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/white" android:state_pressed="true"></item>
    <item android:drawable="@drawable/white" android:state_focused="true"></item>
    <item android:drawable="@drawable/SapphireBlue" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
    <item android:drawable="@drawable/white" android:state_enabled="false"></item>
</selector>

btn_textcolor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/SapphireBlue" android:state_pressed="true"></item>
<item android:color="@drawable/SapphireBlue" android:state_focused="true"></item>
<item android:color="@drawable/white" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:color="@drawable/SapphireBlue" android:state_enabled="false"></item>
</selector>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • 7
    This is correct, here's why it appears to work: The background for a view expects a `Drawable` of some sort. Normally, a color resource is a valid drawable. However, if you use a state list, the parser appears to check for a drawable attribute on the item when looking for a background. This is why the `android:textColor` can point to a selector that has (and must have) `android:color`, and the `android:background` must point to a selector that contains `android:drawable`. Because a color resource is a valid drawable, you can use `android:drawable=@color/...` like you normally would. – karl Jan 10 '14 at 00:23
  • @HareshChhelana HOW CAN I WRITE android:color with hash values like #ffffff – Erum Jan 08 '15 at 06:56
  • and looks like latest android versions allow to use color selectors as background – deviant Nov 23 '20 at 10:10
3
  • Background attribute expects a drawable and the drawable selector must be in one of the /drawable folders.
  • TextColor attribute expects a color and the color selector must be in one of the /color folders.
/res/color/my_textcolor_selctor.xml
/res/drawable/my_background_selector.xml

Also, the drawable selector must use 'android:drawable' for selector items (a color resource maybe used for the attribute value since a color is a drawable), whereas the color selector must use 'android:color' for the selector items.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
        android:drawable="@color/disabledbackgroundColor" />
    <!-- default -->
    <item android:drawable="@color/myBackgroundColor" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
        android:color="@color/disabledColor" />
    <!-- default -->
    <item android:color="@color/defaultColor" />
</selector>
Nicolas
  • 6,611
  • 3
  • 29
  • 73
farid_z
  • 1,673
  • 21
  • 11
2

android:background seems to accept drawable selectors but not color selectors... so just leave a color as your android:background, and use a normal color selector with android:backgroundTint, which expects colors anyway:

at styles.xml:

<style name="MyButton">
    <item name="android:background">@color/some_fixed_color</item>
    <item name="android:backgroundTint">@color/background_color_selector</item>
    <item name="android:textColor">@drawable/btn_textcolor</item>
</style>

(res/color/background_color_selector.xml is a normal color selector.)