2

API 26 introduces an advanced color calculation for ?textColorPrimary based on ?colorForeground. It makes use of states, primaryContentAlpha and disabledAlpha.

sdk/platforms/android-26/data/res/color/text_color_primary.xml:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
        android:alpha="?attr/disabledAlpha"
        android:color="?attr/colorForeground"/>
    <item android:alpha="?attr/primaryContentAlpha"
        android:color="?attr/colorForeground"/>
</selector>

On API 23 it falls back to white text by means I failed to figure out.

Is there a support library I could apply to get the color calculation of API 26 for older devices?

Blcknx
  • 1,921
  • 1
  • 12
  • 38
  • it would be easier to find the other way around –  May 23 '18 at 07:57
  • The other way round? Isn't it the common way to add support libraries to bring new features into older API? – Blcknx May 23 '18 at 07:59
  • The way it's been done until API 23 is that you'd have to provide a different text color selector for each of your themes. Since API 23 theme attributes in colors are supported (and backported). If you find no other alternative you can define your own attribute `primaryContentAlpha` and use it in your app. – Eugen Pechanec May 23 '18 at 08:23
  • Yes, defining my own attribute `primaryContentAlpha` works. Yet it requires to also define my own `color/text_color_primary.xml`. So after all I still have to define the text colours separately. Using `?colorForeground` as central, default color setting seems not to be a mature feature with respect to older devices. – Blcknx May 23 '18 at 10:17
  • None-the-less when working with multiple themes this handmade port-back solution is still interesting and useful. That's exactly my case of usage. – Blcknx May 23 '18 at 10:31

1 Answers1

1

@eugen-pechanec is hinting that the attributes primaryContentAlpha and scondaryContentAlpha are missing, IMHO below API 26. Should we call this a bug or a missing back port? Don't know.

The consequence is that you can't use the setting ?attr/colorForeground as a default to automatically create all foreground colours out of the box. You basically have two options, either not to use it to to do a manual back port.

Disable colorForeground

Instead of generating the colours from ?attr/colorForeground you set the attributes android:textColorPrimary and android:textColorSecondary directly. This will be the best choice in most cases.

Backport colorForeground

If you plan to use a lot of different themes, you want to enable the feature to set defaults for all text colours in a central place. Then you have to implement the behaviour of API 26 in your root theme.

root theme:

    <!-- workaround to port back API 26+ behaviour -->

    <!-- below 26 these two attributes are missing in the android namespace -->
    <item name="primaryContentAlpha">1.0</item> 
    <item name="secondaryContentAlpha">.85</item>
    <!-- works below 26 -->
    <item name="android:disabledAlpha">.4</item>
    <!-- use my own files to connect my custom attributes -->
    <item name="android:textColorPrimary">@color/text_color_primary</item>
    <item name="android:textColorSecondary">@color/text_color_secondary</item> 

app/src/main/res/color/text_color_primary.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:alpha="?android:disabledAlpha" android:color="?android:attr/colorForeground" />
    <item android:alpha="?attr/primaryContentAlpha" android:color="?android:attr/colorForeground" />
</selector>

app/src/main/res/color/text_color_secondary.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:alpha="?android:disabledAlpha" android:color="?android:colorForeground"/>
    <item android:alpha="?secondaryContentAlpha" android:color="?android:colorForeground"/>
</selector>
Lorne Laliberte
  • 6,261
  • 2
  • 35
  • 36
Blcknx
  • 1,921
  • 1
  • 12
  • 38