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.
Related
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.
I am trying to use vector drawables as icons of BottomNavigationView like following:
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_incoming"
android:icon="#drawable/incoming_background"
android:title="Incoming"/>
<item
android:id="#+id/navigation_outgoing"
android:icon="#drawable/outgoing_background"
android:title="Outgoing"/>
<item
android:id="#+id/navigation_important"
android:icon="#drawable/favorite_background"
android:title="Favourite"/>
<item
android:id="#+id/navigation_settings"
android:icon="#drawable/settings_background"
android:title="Settings"/>
</menu>
And my drawables are like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/ic_incoming_active"/>
<item android:drawable="#drawable/ic_incoming_inactive"/>
</selector>
#drawable/ic_incoming_active and #drawable/ic_incoming_inactive are both vector drawables.
I am also using vectorDrawables.useSupportLibrary = true
and
implementation "com.android.support:support-vector-drawable:27.1.1"
But I am getting
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_incoming_active.xml from drawable resource
The error means that the drawable ic_incoming_active.xml can't be found.
In most cases the reason is that you have stored this drawable in a folder like drawable-v21 and not in res/drawable.
Here is my selector
<?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/black"/>
<item android:color="#color/white"/>
</selector>
apparently I cannot save it in the /drawable folder as my android won't compile. Then if I create a /color folder in res I get a red mark from eclipse. So where do I save my selector so I can use it as the background color of a TextView (or really any other view).
I have a colors.xml file in values, but how would I add a selector to it?
THE ACTUAL SOLUTION
I am posting this edit in case someone else needs help. I hope you this saves you some time.
For the correct answer, I did following
In strings.xml
<drawable name="black_drawable">#color/black</drawable>
<drawable name="white_drawable">#color/white</drawable>
In colors.xml
<color name="black">#000000</color>
<color name="white">#ffffff</color>
Then in the selector, I did
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/black_drawable" android:state_pressed="true"/>
<item android:drawable="#drawable/white_drawable"/>
</selector>
The selector is saved in /drawable as selector_black_white.xml
Here's a solution I found.
You just need to create a "color" folder in your res and save the file in the newly created folder.
After completing this step you will be able to get the color selector file in your attribute of respective xml file.
I'm attaching screenshots for better understanding.
Step 1
Step 2
Last edit i messed up pretty in my previous edit sorry my bad this one is working now
res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/whiteColor"/>
<item android:drawable="#color/backColor"/>
</selector>
then on your res/value/string or create new values color
<color name="whiteColor">#000000</color>
<color name="backColor">#ffffff</color>
I think your problem is not the location - it should be in the /res/color folder. The problem is in how you specify the color:
<item android:color="#color/ffffff"/>
should be
<item android:color="#ffffff"/>
I'm developing an android project, and I want to use a <selector> in an XML file. I've put the <selector> in a file under res/drawable/default_item_background_selector.xml, and I'm referencing it with XML attributes like
<TextView
android:background="#drawable/default_item_background_selector"/>
The XML attribute content I get from Eclipse's content assist, so that can see it just fine. However, when I compile everything (and it compiles just fine) and debug it on either simulator or on a device, the app crashes, with a root exception of :
09-24 23:55:14.771: E/AndroidRuntime(22478): Caused by:
android.content.res.Resources$NotFoundException: File
res/drawable/default_item_background_selector.xml from drawable
resource ID #0x7f020000
The R.drawable.default_item_background_selector gets generated just fine, but at runtime, it appears there's no physical file generated in the output directory. Has anybody experienced this before ? Yes, I've cleaned and recompiled (so many times).
Created default_bg.xml in Drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true"
android:drawable="#drawable/red" />
<item android:state_pressed="true" android:state_selected="false"
android:drawable="#drawable/blue" />
<item android:state_selected="false"
android:drawable="#drawable/yellow" />
</selector>
Create drawables in **strings.xml** is
<drawable name="blue">#0000FF</drawable>
<drawable name="red">#FF0000</drawable>
<drawable name="yellow">#FFFF55</drawable>
And set background to textview
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:background="#drawable/default_bg"
/>
<?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....