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 w
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.)
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>
/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>