title: Change text color of Material TextView on surface and on background through theming
body: <p>I'm following Material Design's <a href="https://material.io/develop/android/theming/color/" rel="nofollow noreferrer">guidelines</a> on how to do color theming. We can alter some attributes that are already defined to customize the color and/or shape of a certain component. Some useful attributes that are pertinent to color theming include: <code>colorSurface</code> (e.g. changes the fill color of the <code>Material Card</code> component), <code>colorSecondary</code> (e.g. changes the fill color of a FAB) and <code>colorOnSecondary</code> (e.g. changes the fill color of the FAB icon).</p>
  
  <p>Currently, I have two resource directories: <code>values</code> and <code>values-night</code> for light-themed and dark-themed UI respectively. They control the value of the colors that are assigned to the color attributes. Codes:</p>
  
  <pre><code>&lt;!-- Light-themed UI color values --&gt;
  &lt;?xml version="1.0" encoding="utf-8"?&gt; 
  &lt;resources&gt;
      &lt;color name="colorPrimary"&gt;#546e7a&lt;/color&gt;
      &lt;color name="colorPrimaryVariant"&gt;#29434e&lt;/color&gt;
      &lt;color name="colorOnPrimary"&gt;#ffffff&lt;/color&gt;
      &lt;color name="colorSecondary"&gt;#d81b60&lt;/color&gt;
      &lt;color name="colorSecondaryVariant"&gt;#a00037&lt;/color&gt;
      &lt;color name="colorOnSecondary"&gt;#000000&lt;/color&gt;
      &lt;color name="colorSurface"&gt;#ffffff&lt;/color&gt;
  &lt;/resources&gt;
  
  &lt;!-- Dark-themed UI color values --&gt;
  &lt;?xml version="1.0" encoding="utf-8"?&gt; 
  &lt;resources&gt;
      &lt;color name="colorPrimary"&gt;#546e7a&lt;/color&gt;
      &lt;color name="colorPrimaryVariant"&gt;#29434e&lt;/color&gt;
      &lt;color name="colorOnPrimary"&gt;#ffffff&lt;/color&gt;
      &lt;color name="colorSecondary"&gt;#d81b60&lt;/color&gt;
      &lt;color name="colorSecondaryVariant"&gt;#a00037&lt;/color&gt;
      &lt;color name="colorOnSecondary"&gt;#000000&lt;/color&gt;
      &lt;color name="colorSurface"&gt;#000000&lt;/color&gt;
  &lt;/resources&gt;
  
  &lt;!-- Style resource file that changes in accordance to current default night mode --&gt;
  &lt;style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"&gt;
      &lt;item name="colorPrimary"&gt;@color/colorPrimary&lt;/item&gt;
      &lt;item name="colorPrimaryDark"&gt;@color/colorPrimaryVariant&lt;/item&gt;
      &lt;item name="colorOnPrimary"&gt;@color/colorOnPrimary&lt;/item&gt;
      &lt;item name="colorSecondary"&gt;@color/colorSecondary&lt;/item&gt;
      &lt;item name="colorOnSecondary"&gt;@color/colorOnSecondary&lt;/item&gt;
      &lt;item name="colorSurface"&gt;@color/colorSurface&lt;/item&gt;
      &lt;item name="colorOnSurface"&gt;@color/colorOnSurface&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>Text on some components (e.g. buttons) is affected by <code>colorOnPrimary</code>. When the components uses both <code>colorPrimary</code> and <code>colorOnPrimary</code>, I can control the colors used well. However, components' color like <code>Material TextView</code>'s color does not depend on <code>colorOnSurface</code> and instead can be modified through <a href="https://material.io/develop/android/theming/typography/" rel="nofollow noreferrer">special attributes</a> like <code>textAppearanceHeadline1</code>. Of course, I can simply modify <code>android:textColor</code> on <code>textAppearanceHeadline1</code> to follow the color of <code>colorOnSurface</code>. However, <code>Material TextView</code> does sometimes appear on background (not surfaces) and when this happens, I want the <code>android:textColor</code> to have the value <code>colorOnBackground</code> instead.</p>
  
  <p>I can probably just apply the style with <code>colorOnSurface</code> to all <code>Material TextView</code> and only override the style with <code>colorOnBackground</code> when a <code>Material TextView</code> does appear on the background. However, is there any way that I can change the value used by <code>android:textColor</code> automatically depending on where the text appears?</p>