Selector requires drawable attribute? - android

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ff0000"/>
<item android:state_focused="true"
android:color="#0000ff"/>
<item android:color="#00ff00"/>
</selector>
I have this selector which I'm trying to use to change the background of a Linearlayout. Whenever I try to apply it, however I always get this error message:
org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: tag requires a 'drawable' attribute or child tag defining a drawable
Obviously, it want me to use the drawable attibute but I'm not sure how to do that and change the background like I want to.

android:drawable="#color/red"
and add this in every item with diffrent color....

Related

How to get drawable resource from boolean resource

Is there a way to get a drawable resource from a boolean resource?
For example:
bools.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="use_version_1_drawables">true</bool>
</resources>
my_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/my_drawable_version_1"/>
<item android:drawable="#drawable/my_drawable_version_2"/>
</selector>
Is there a specific state I should be using because from what I understand they are all related to specific events (checking, focusing, etc). Perhaps I shouldn't be using a selector. I simply want to have one resource I can call upon that's actually linking to two others but will select one based on my bool.
You are close. You can think of a selector as an if/else statement. What you're missing now is your actual state. Here is an example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimaryDark" android:state_checked="true" />
<item android:color="#color/gray" android:state_checked="false" />
</selector>
If you're using it with something like a Checkbox, then just set this selector as a drawable resource for the checkbox, and state_checked will be updated automatically.
EDIT: Problem was slightly more complex, and solved in the comments.

What is the different between getColor() and getColorStateList() in Android

I'm using getColor() method to select color from resources. But I found that there is another method called getColorStateList(). Which one is easy to use and what is the difference between them?
lets assume that you want to setBackgroundColor to a view for example a linearLayout.
if you want its background color to be permanent ,you would want to use getColor() to set a certain color.
but if you want its color to change on different states and events like pressed state or not pressed state you would want to set resource id of the xml file containing the code for these color change tasks.
here is what im saying in code:
linearLayout.setBackgroundColor(getResources().getColor(R.color.red);
line of code above sets the permanent color of linearLayout to red.
linearLayout.setBackgroundTintList(getResources().getColorStateList(R.drawable.layout_background));
and this single line of code above will set the background color to red when layout is pressed and white when its not pressed.
layout_background.xml :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/red" />
<item android:state_pressed="false"
android:color="#color/white" />
</selector>
getColor()
Returns a color integer associated with a particular resource ID
getColorStateList()
ColorStateLists are created from XML resource files defined in the "color" subdirectory directory of an application's resource directory. The XML file contains a single "selector" element with a number of "item" elements inside. For example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:color="#color/sample_focused" />
<item android:state_pressed="true"
android:state_enabled="false"
android:color="#color/sample_disabled_pressed" />
<item android:state_enabled="false"
android:color="#color/sample_disabled_not_pressed" />
<item android:color="#color/sample_default" />
</selector>

How to apply 2 different kinds of styles to a single Android control?

Say I have this:
values/styles.xml
<resources>
<style name="MyEditText">
<item name="android:maxLines">1</item>
........
And this (not in a file yet):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#000000" />
</shape>
And an EditText.
In which file should I put the 2nd piece of the code?
How can I apply these 2 styles to a single EditBox?
Updated
The properties that are defined a style xml file and applied to a view ( by setting style="#style/my_custom_style" can be overridden.
For example, if in the style file I have defined a background, I can re-assign that property in the xml layout of that view again, or even programmatically.
Consequently, if for the example view, I set both style="#style/my_custom_style" (including background property with the value of drawable_one) and android:background="#drawable/**drawable_two**", then eventually the drawable_two will take effect.
In the style.xml file add the background item
<item name="android:background">#drawable/your_drawable_xml</item>
And for its value (your_drawable_xml), use the second drawable xml.
Now you can apply both in the layout declaration of EditText:
style="#style/MyEditText"

I get error when use android:background and item_bg.xml

I try to create a selectable Listview.
Everthing is right but i get error when i use the android:background="#drawable/item_bg"
drawable/item_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true"
android:color="#999999">
</item>
<item android:state_pressed="true"
android:color="#FF00FF">
</item>
<item android:state_selected="true"
android:color="#b3bdff">
</item>
<item android:state_pressed="false"
android:color="#000000">
</item>
</selector>
Error lines:
android.view.InflateException: Binary XML file line #1: Error
inflating class
Caused by: android.content.res.Resources$NotFoundException: File
res/drawable-hdpi-v4/item_bg
If I deleted all item tag it work but not to be selectable
it solved!
deleted all drawable files! deleted attribute what name
android:background="#drawable/item_bg" created xml again but with
different name!(e.g. item_bg_selector) created colors.xml define
attibute again (android:background="#drawable...")
it's done!
When you create a new xml file (Color, gradient...) into drawable folder and then use this file with any attribute into layout you write without extensión.

Drawable resources and text color

I am using drawables to nicely style my buttons, and that works fine, except for the text color in the button.
I have defined a state_enabled="false" item in a selector and using setEnabled gives me the right button styles, but I have to jump through quite some loops to get the text color different. This code for example doesn't work (it shows no, or black, text when disabled, and darkgray when enabled):
public void setButtonsEnabled(boolean enable) {
btnAccept.setEnabled(enable);
btnDecline.setEnabled(enable);
int color = R.color.White;
if (!enable) {
color = R.color.DarkGray;
}
btnAccept.setTextColor(color);
btnDecline.setTextColor(color);
}
I found the solution.
The key lies in also setting the TextColor to a selector in res/colors:
android:textColor="#color/button_text"
android:background="#drawable/button_selector"
For the background selector I used 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="false" android:drawable="#drawable/btn_buddy_enabled"></item>
<item android:state_enabled="false" android:drawable="#drawable/btn_buddy_disabled"></item>
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#drawable/btn_buddy_clicked"></item>
</selector>
And the textColor selector is 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="false" android:color="#color/White"></item>
<item android:state_enabled="false" android:color="#color/Gray"></item>
<item android:state_enabled="true" android:state_pressed="true" android:color="#color/White"></item>
</selector>
Simply calling setEnabled() will make everything work fine.
You are using the wrong value for the color. R.color.White returns the resource ID of the value, not the value itself. Try Color.WHITE, or getResources().getColor(R.color.White)
Have you checked out ColorStateLists? They are pretty awesome. So basically apply all those ideas of Drawable selectors to a set of colors.
Make a folder called [Your Project]/res/colors/ and then put an xml file in there called, button_color.xml (or whatever).
button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- Any Enabled button, gets White Text -->
<item
android:color="#color/White"
android:state_enabled="true" />
<!-- Buttons with any other state, get DarkGray Text -->
<item
android:color="#color/DarkGray"/>
</selector>
And then for your TextView, you can just do something like, mTextView.setTextColor(R.color.button_color); At that point there is no need for that if/else type of logic, the selector will do it for you. The selector gets rolled up into the color resource but the class it actually generates is called a ColorStateList in case you find it referenced in other documentation.

Categories

Resources