title: Set theme color dynamically
body: <p>I am using themes (dynamically) in my android app, like this:</p>
  
  <p><strong>my_layout.xml</strong> (extract):</p>
  
  <pre><code>&lt;TextView
      android:id="@+id/myItem"
      style="?my_item_style" /&gt;
  </code></pre>
  
  <p><strong>attrs.xml</strong> (extract):</p>
  
  <pre><code>&lt;attr name="my_item_style" format="reference" /&gt;
  </code></pre>
  
  <p><strong>themes.xml</strong> (extract):</p>
  
  <pre><code>&lt;style name="MainTheme.Blue"&gt;
        &lt;item name="my_item_style"&gt;@style/my_item_style_blue&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name="MainTheme.Green"&gt;
        &lt;item name="my_item_style"&gt;@style/my_item_style_green&lt;item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p><strong>styles.xml</strong> (extract):</p>
  
  <pre><code>&lt;style name="my_item_style_blue"&gt;
        &lt;item name="android:textColor"&gt;@color/my_blue&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name="my_item_style_green"&gt;
        &lt;item name="android:textColor"&gt;@color/my_blue&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>So, as you can see, I am setting themes dynamically. I am using this class:</p>
  
  <pre><code>public class ThemeUtils {
  
    private static int sTheme;
    public final static int THEME_BLUE = 1;
    public final static int THEME_GREEN = 2;
  
    public static void changeToTheme(MainActivity activity, int theme) {
        sTheme = theme;
        activity.startActivity(new Intent(activity, MyActivity.class));
    }
  
    public static void onActivityCreateSetTheme(Activity activity)
    {
        switch (sTheme)
        {
            default:
            case THEME_DEFAULT:
            case THEME_BLUE:
                activity.setTheme(R.style.MainTheme_Blue);
                break;
            case THEME_GREEN:
                activity.setTheme(R.style.MainTheme_Green);
                break;
        }
    }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>What I want to know, is there a way how to do this (change theme color) in code?</strong> For example, I have following code (extract):</p>
  
  <pre><code>((TextView) findViewById(R.id.myItem)).setTextColor(R.color.blue);
  </code></pre>
  
  <p>It can be done by some helper method, which would use <code>switch</code> command for available themes and return correct color for a theme. But I would like to know if there is some better, nicer and faster way.</p>
  
  <p>Thanks!</p>