<item> tag requires a 'drawable' attribute - android

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.

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>

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>

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

Related

How to change active Bottom bar tab background color

I am using roughike/BottomBar below link shown to a image so how am i make look like this.
https://i.stack.imgur.com/Wybr2.png please help
You can add in your styles.xml :
<item name="android:navigationBarColor">#color/theme_color</item>
or programmatically
window.setNavigationBarColor(#ColorInt int color)
Of course you should bear in mind that this will only work with API>21.
Also refer to this link.
Define a selector drawable and also make two drawable for selected and unselected tab
selector tab tab_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/tab_background_selected" android:state_selected="true" />
<item android:drawable="#drawable/tab_background_unselected" android:state_selected="false" android:state_focused="false" android:state_pressed="false" />
</selector>
tab_background_selected.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#d13fdd1a" />
</shape>
tab_background_unselected.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#3F51B5" />
</shape>
And after all that in your style.xml mention your selector to use
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabBackground">#drawable/tab_background</item>
<item name="tabIndicatorColor">#ff00ff</item>
<item name="tabIndicatorHeight">2dp</item>
</style>

Buttons With Background Color and Image

I have a button that changes the image when it clicked.
I've done that with this code.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_fullscreen_gmap" android:state_selected="false"/>
<item android:drawable="#drawable/ic_fullscreen_exit_gmap"/>
</selector>
Is it possible to change the color of the default button but Together with the Image?
I want to change it to something.
The default button color is something like gray and I want to change that.
To change both the image and the background color of your Button, you could use an ImageButton and define separate selectors for the android:src and android:background attributes.
For example:
button_src_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_fullscreen_gmap"/>
<item android:state_pressed="true"
android:drawable="#drawable/ic_fullscreen_exit_gmap"/>
</selector>
button_bg_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/color1"/>
<item android:state_pressed="true"
android:drawable="#color/color2"/>
</selector>
And the ImageButton:
<ImageButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/button_src_selector"
android:background="#drawable/button_bg_selector"/>
Following code will help you to change default button background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/button_bg_normal"></item>
</selector>

Change the color of a disabled button in android

Is there a way to change the color of a disabled button in android through styles or some other form ?
I currently have the following,
drawable/button_default.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_default_shape"/>
</selector>
drawable/button_default_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/button_default_background"/>
<corners android:radius="3dp"/>
</shape>
values/styles.xml
<style name="AppTheme.Button">
<item name="android:background">#drawable/button_default</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textAllCaps">false</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">17sp</item>
<item name="android:textAppearance">#style/CustomFontAppearance</item>
</style>
You'll have to use a selector for different drawables in those states.
You can create a selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/your_enabled_drawable" android:state_enabled="true" />
<item android:drawable="#drawable/your_disabled_drawable" android:state_enabled="false" />
<!-- default state-->
<item android:drawable="#drawable/your_enabled_drawable" />
</selector>
Specify the color in selector for android:state_enabled="false" as drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<solid android:color="#32ff09" />
</shape>
</item>
</selector>
And apply this drawable resource to background of button
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/drawble_name"
android:enabled="false"
android:text="Selector Applied Button" />
Try this -
drawable/bg_button.xml :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_button_focused" android:state_selected="true"/>
<item android:drawable="#drawable/bg_button_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/bg_button_disabled" android:state_enabled="false" />
<item android:drawable="#drawable/bg_button_normal"/>
</selector>
After this just set background on your button like this -
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_button"
android:text="Button"/>
UPDATE: For Android MaterialButton
For Android MaterialButton there are two solutions, one for SDK >= v21 and another for SDK < v21.
Here I am also giving how to change the text color of the button depending whether it is enabled or disabled.
First create two color selector .xml file in your color folder. One for button color and another for button text color. For example button_state_colors.xml for button color and button_text_state_colors.xml for text color.
Here are those two color files:
button_state_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#303F9F" />
<item android:state_enabled="false" android:color="#BBBBBB" />
</selector>
button_text_state_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#FFFFFF" />
<item android:state_enabled="false" android:color="#555555" />
</selector>
Now for all versions of Android SDK you can change the color of button and it's text using style like:
in style.xml:
<style name="Widget.AppTheme.MaterialButton" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">#color/button_state_colors</item>
<item name="android:textColor">#color/button_text_state_colors</item>
</style>
in layout.xml:
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
style="#style/Widget.AppTheme.MaterialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Only for SDK >= 21 you can direct change button color without using style like:
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/button_state_colors"/>
Another way to achieve this is to create a color selector.
Create a file
res/color/your_color.xml
which looks like
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorDisabledBackground" android:state_enabled="false" />
<item android:color="#color/colorEnabledBackground" />
</selector>
Then you may use it as a regular color
in a style:
<style name="YourStyle">
<item name="android:background">#color/your_color</item>
<item name="android:textColor">#android:color/black</item>
</style>
As well is in layouts, shapes or in code as etc.
Here is my code which works properly with button enable and disable state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="#drawable/ic_button_gradient"/>
<item android:state_enabled="false" android:drawable="#color/gray"/>
</selector>

selector for listview doesn't work

I want to set the color of row in listview if selected to yellow and otherwise be white so I use the following selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_selected="true" android:drawable="#color/encounterselector_color" />
<item
android:drawable="#color/encounter_normal" />
</selector>
where
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="encounterselector_color">#fbeda5</color>
<color name="encounter_normal">#ffffff</color>
</resources>
and I use it like the following
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/EncounterGrid"
android:background="#drawable/encounterlistview"
>
<!-- remaining code -->
but the row is always white , any idea how to fix that
Setting the background color with a selector is a bit tricky. Basically you have to create a drawable for each color and use them in your android:drawable attributes. You cannot directly use colors.
Check this related question for more details.
I use the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the switched off state -->
<item android:state_enabled="false"
android:drawable="#drawable/grey_bar" />
<!-- These are the partial states -->
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/button_focused" />
<!-- This is the switched on state -->
<item android:state_enabled="true"
android:drawable="#drawable/button_normal" />
</selector>
Where all of the drawables I point to are defined in xml, or are existing 9 patch images.
use android:color on your selector and not android:drawable because you are getting reference to the color resources, so your selector will be like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:color="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_focused="true" android:color="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_selected="true" android:color="#color/encounterselector_color" />
<item
android:color="#color/encounter_normal" />
</selector>
Nothing was working for me until I set drawSelectorOnTop = "true".
Everything worked after that.

Background color from XML

I have a layout and i want to set your background color with this xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:color="#000000"></item>
<item android:color="#ffffff" ></item>
</selector>
this layout to be in a tabhost and where is selected it change your olor, how I do?
There's three "main" states you need to take into account: selected, pressed and the rest. Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/tab_background_active" />
<item android:state_pressed="true" android:drawable="#color/tab_background_pressed"/>
<item android:drawable="#color/tab_background_inactive" />
</selector>
You should also use "focused" if you care about supporting users with trackballs.

Categories

Resources