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?
@eugen-pechanec is hinting that the attributes
primaryContentAlpha
andscondaryContentAlpha
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
colorForground
Instead of generating the colours from
?attr/colorForeground
you set the attributesandroid:textColorPrimary
andandroid:textColorSecondary
directly. This will be the best choice in most cases.Backport
colorForground
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
:app/src/main/res/color/text_color_primary.xml
:app/src/main/res/color/text_color_secondary.xml
: