1

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.

DiscDev
  • 38,652
  • 20
  • 117
  • 133

1 Answers1

0

You can do it in java code, using setBackgroundDrawable(new ColorDrawable(color)). It can't be done in xml.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Can you site a source for this @Gabe Seechan ? I use this on 100+ xml pages and I am trying to avoid having to call a method in every one of the Activities or Fragments. – DiscDev Jul 09 '15 at 00:33
  • Well, for starters the xml parsing system doesn't know what a shared preference is. There's no linkage between the two. – Gabe Sechan Jul 09 '15 at 00:36
  • Right - I was hoping there was some trick I could pull, using a custom View class that extends BitmapDrawable and check the magic preferences in there, then set a specific jpg as the "src" depending on the preferences? – DiscDev Jul 09 '15 at 00:37
  • 1
    If you're willing to go to that level- ok. You can create a new class that derives from linear layout, and override all 3 of its constructors to override the background *after* calling super. But then anytime you want to use that background color, you'll have to use that class instead of linear layout- and change ALL of your xml to do so. And then do the same for relative layout if necessary And FrameLayout if necessary. And probably ListView, if necessary. You're looking at a lot of work either way. – Gabe Sechan Jul 09 '15 at 00:40
  • Ouch. In iOS I ended up creating a UIViewController subclass and basically doing what you suggested doing with a LinearLayout subclass...part of the problem is that I want to "theme" more than just LinearLayouts - Buttons, etc. so I was hoping to make a reusable smart bitmap widget with XML to make my current solution work with very little modification...bummer that it isn't possible this way. – DiscDev Jul 09 '15 at 02:30