androidxmltextviewtextcolor

How to set default android:textColorLink


I am attempting to set an app-wide style for my TextViews that has a specific color for links. If I set android:textColorLink on each individual TextView, it works fine. However, if I set android:textColorLink inside a style in styles.xml, it continues displaying the normal android:textColor. Has anyone else experienced this or know of a solution? I would prefer to set it as the style so I do not need to update multiple references if the color were to change in the future.


The following method of individually setting android:textColorLink for each TextView does work and will display color1 for normal text and color2 for links.

styles.xml:

<style name="textStyle" parent="Widget.AppCompat.TextView">
    <item name="android:textColor">@color/color1</item>
</style>

Layout file:

<TextView
    android:textAppearance="@style/textStyle"
    android:textColorLink="@color/color2" />

If I instead try just updating the style to include android:textColorLink instead of listing it on each TextView, it does not work and will display color1 for all normal text and links.

styles.xml:

<style name="textStyle" parent="Widget.AppCompat.TextView">
    <item name="android:textColor">@color/color1</item>
    <item name="android:textColorLink">@color/color2</item>
</style>

Layout file:

<TextView
    android:textAppearance="@style/textStyle" />

Solution

  • A coworker helped me find the solution to this soon after posting. I had assumed that android:textColorLink would be able to be set through android:textAppearance since I am already setting android:textColor the same way. However, it would appear that android:textColorLink can only be set through style. I am not certain why there is this difference in the ways the colors are applied through styles. If anyone more knowledgeable knows why, please let me know.


    For example, this works.

    styles.xml:

    <style name="textStyle1" parent="Widget.AppCompat.TextView">
        <item name="android:textColor">@color/color1</item>
    </style>
    <style name="textStyle2" parent="Widget.AppCompat.TextView">
        <item name="android:textColorLink">@color/color2</item>
    </style>
    

    Layout file:

    <TextView
        android:textAppearance="@style/textStyle1"
        style="@style/textStyle2" />
    

    This can also be cleaned up a bit so you only need to set style on the TextViews.

    styles.xml:

    <style name="textStyle" parent="Widget.AppCompat.TextView">
        <item name="android:textColor">@color/color1</item>
        <item name="android:textColorLink">@color/color2</item>
    </style>
    

    Layout file:

    <TextView
        style="@style/textStyle" />