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.
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 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.
i'm try to apply actionbar in my android, i copied a menu xml from another app to current app (/res/layout/menu/actionbarmenu.xml)
here is the code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/media_play"
android:icon="#android:drawable/ic_media_play"
android:title="#string/media_play"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/media_pause"
android:icon="#android:drawable/ic_media_pause"
android:title="#string/media_pause"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/media_previous"
android:icon="#android:drawable/ic_media_previous"
android:title="#string/media_previous"
android:showAsAction="ifRoom" />
<item android:id="#+id/media_next"
android:icon="#android:drawable/ic_media_next"
android:title="#string/media_next"
android:showAsAction="ifRoom">
</item>
</menu>
In every line "ifRoom|withText", there is a red underline appear and with yellow box :
Should use app:showAsAction with the appcompat library with
xmlns:app="http://schemas.android.com/apk/res/auto"
Anyone know what is the problem?
You should use app namespace for menu which is part of appcompat.
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/auto">
<item android:id="#+id/media_play"
android:icon="#android:drawable/ic_media_play"
android:title="#string/media_play"
app:showAsAction="ifRoom|withText"/>
<item android:id="#+id/media_pause"
android:icon="#android:drawable/ic_media_pause"
android:title="#string/media_pause"
app:showAsAction="ifRoom|withText"/>
<item android:id="#+id/media_previous"
android:icon="#android:drawable/ic_media_previous"
android:title="#string/media_previous"
app:showAsAction="ifRoom" />
<item android:id="#+id/media_next"
android:icon="#android:drawable/ic_media_next"
android:title="#string/media_next"
app:showAsAction="ifRoom">
</item> </menu>
The android:showAsAction has been added in API 11 and your app has probably lower minimum API level. You can use the AppCompat library which will provide this parameter and its desired behavior, however, to reffer the AppCompat parameter you have to call it within the right XML namespace.
The namespace is included by the suggested code added to root <menu> tag
xmlns:app="http://schemas.android.com/apk/res/auto"
and then you can add app:showAsAction to your <item> tags
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"/>
<?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....