Question: Is there a way to dynamically set the "src" attribute on an XML "bitmap" in an Android XML file?
I have a custom XML drawable named "repeat_background.xml" defined as such:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background"
android:tileMode="repeat" />
I use this drawable to "tile" a 1x1 jpg (named background.jpg) as the background for all the pages in my app, and it works great - here's an example setting it as the background of a LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/repeat_background">
However, I want the actual jpg (in this case "background.jpg" from @drawable/background) to be based on the user's preference - so I'll have a list that allows the user to pick red, blue, orange, etc. so the user can override the background color shown in the app, and I'll have a 1x1 jpg corresponding to each available color in my resource bundle - but how do I show the preferred jpg as the background?
I don't want to manually have to call some code in every Fragment or Activity, I want something that will respect the user's preferences and react accordingly.
I've tried to extend the BitmapDrawable class, but didn't get very far. Any suggestions for how I can accomplish this are greatly appreciated.