0

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.

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
Kent J.
  • 53
  • 7
  • Which way are you planning to variate the value of `use_version_1_drawables`? – art Jan 17 '20 at 20:32
  • @art I'm writing a module so I'm intending that when the module is used that all you have to do is add a boolean with id of `user_version_1_drawables` and set it to whatever you want. – Kent J. Jan 17 '20 at 20:36
  • You can use arrays (in xml) of drawable id's. Each array would have two entries, position 0 for false and position 1 for true. See for example [this post](https://stackoverflow.com/questions/29819204/could-android-store-drawable-ids-like-an-integer-array) for how to get the actual drawables – Bö macht Blau Jan 17 '20 at 21:20

1 Answers1

0

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.

Khodor
  • 129
  • 1
  • 7
  • I'm actually using the drawable as an icon for a navigation item so the `android:state_checked` state doesn't work for me. I don't want to change the drawable based on a checked state, but instead want to just use either or based on my boolean. – Kent J. Jan 17 '20 at 20:25
  • Is this view a custom class you created? Or is it from some navigation component? – Khodor Jan 17 '20 at 20:35
  • It's just for icons in a menu. The menu is being used for a bottom navigation view. – Kent J. Jan 17 '20 at 20:37
  • Hm. I'll think of something. Best thing I can think of now is inflating different menus depending on that boolean, but that just doesn't seem very right. – Khodor Jan 17 '20 at 20:42
  • Yeah that's the kind of thing I'm trying to avoid. – Kent J. Jan 17 '20 at 20:46
  • Is defining it as a theme attribute out of the question aswell? – Khodor Jan 17 '20 at 20:47
  • I hadn't considered that before. I created some custom attributes for my drawables and then setup two app themes (one for version one and one for version two) and it works exactly as expected. – Kent J. Jan 17 '20 at 20:59
  • Yes, that was exactly what I was thinking. :) – Khodor Jan 17 '20 at 21:02