title: Programmatically set an image based on theme in Android
body: <p>I want to set an ImageView's image source based on current theme. I have two themes defined, and each has an icon for the ImageView.</p>
  
  <p>I have the following in <code>themes.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;style name="Theme.Android"&gt;
          &lt;item name="tabIconContacts"&gt;@style/tabContacts_Android&lt;/item&gt;
      &lt;/style&gt;
      &lt;style name="Theme.Earthy"&gt;
          &lt;item name="tabIconContacts"&gt;@style/tabContacts_Earthy&lt;/item&gt;
      &lt;/style&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>in <code>attr.xml</code>:</p>
  
  <pre><code>&lt;attr name="tabIconContacts" format="reference" /&gt;
  </code></pre>
  
  <p>and the following in <code>styles.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;style name="tabContacts_Earthy"&gt;
          &lt;item name="android:src"&gt;@drawable/theme1_tab_contacts&lt;/item&gt;
      &lt;/style&gt;
      &lt;style name="tabContacts_Android"&gt;
          &lt;item name="android:src"&gt;@drawable/theme2_tab_contacts&lt;/item&gt;
      &lt;/style&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>and I apply it with <code>setTheme()</code> at the top of <code>onCreate()</code>. Setting the background image on resources defined in XML works fine. I'm struggling to find a nice way of setting the image in the following code:</p>
  
  <pre><code>LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  View tabIndicator = li.inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
  ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon);
  iconView.setImageResource( R.style.tabIconContacts ));
  </code></pre>
  
  <p>Is it possible to swap out drawable resources in this way? I found a lot of articles which mention <code>obtainStyledAttributes</code>, but they don't seem to apply to this situation.</p>
  
  <p>I also can't set the image in the XML definition of tab_indicator because the icon changes with every tab and every theme (around 12 icons in total).</p>
  
  <p>Any help will be greatly appreciated.</p>