Reputation: 33511
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.
Upvotes: 8
Views: 17990
Reputation: 1673
/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>
Upvotes: 3
Reputation: 6037
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.)
Upvotes: 2
Reputation: 24848
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>
Upvotes: 24