Android: textColor ignored in android:textAppearance of the EditText
Asked Answered
T

3

14

Is there any reason why is textColor property being ignored when it is set via custom textAppearance style?

<style name="EditTextAppearance" parent="@android:style/TextAppearance.Widget.EditText">
    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/blue</item> <!-- IGNORED -->
</style>

Setting style in XML:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@style/EditTextAppearance"
    />

For some reason, default theme control color is not overridden by this style.

The only way how I am able to set color is by setting textColor property in EditText (but this is not what I want):

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@style/EditTextAppearance"
    android:textColor="@color/blue"
    />

Note, custom textAppearance with textColor created for TextView works without problem.

I tried to replace EditText by android.support.v7.widget.AppCompatEditText or android.support.design.widget.TextInputEditText but the result was the same. Still not working. So the problem is not in EditText implementation.

I found question with the same problem Why is textColor in android:textAppearance ignored?. Unfortunately, without answer.

Thicken answered 19/7, 2017 at 16:34 Comment(0)
A
19

Somewhere along the line, the wrong/default value for textColor is being picked up and applied. You can force the android:textColor that you define in android:textAppearance to be used by setting android:textColor="@null" in your XML for EditText.

Auspicate answered 19/7, 2017 at 18:55 Comment(2)
I did not expect that soultion can be this simple. Thank you!Thicken
the sad thing about this is that you'd have to always remember to add android:textColor="@null"..Gratitude
K
1

android:textColor ignored in android:textAppearance of the EditText because android:editTextColor attribute always overrides text color of the EditText (don ask me why, its about crazy droid).

But all works fine if you add this attribute to your theme:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
     <item name="android:editTextColor">@null</item>
</style>
Kendyl answered 21/7, 2021 at 21:17 Comment(0)
T
0

Still don't know why is textAppearance ignoring textColor when style is created as described in question, but I think I have found another way how to set textColor via style, so somebody might find it useful.

I was playing with EditText colors so I've created custom theme and tried to set editTextColor property, which surprisingly worked:

<!-- consider to extend application theme over default "Base.V7.Widget.AppCompat.EditText" to achieve consistent look and feel -->
<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
    <item name="colorControlNormal">@color...</item>    <!-- underline color -->
    <item name="colorControlActivated">@color...</item> <!-- underline color on focus -->
    <item name="editTextColor">@color/blue</item> <!-- ACCEPTED -->
</style>

Setting theme:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@style/EditTextAppearance"
    android:theme="@style/EditTextTheme"
    />

At this moment you have custom "@style/EditTextAppearance" and "@style/EditTextTheme". The good thing is, that you can merge "@style/EditTextAppearance" into the "@style/EditTextTheme" as follows:

<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
    ...
    <item name="editTextColor">@color/blue</item>
    <item name="editTextStyle">@style/EditTextAppearance</item>
</style>

This way, you just have to set only theme in your EditText:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/EditTextTheme"
    />

Current solution is almost perfect, but it would be better, if textColor will be set in textAppearance style, so I've tried to remove editTextColor from theme and replace it by textColor property set in textAppearance style @style/EditTextAppearance.

<style name="EditTextAppearance" parent="@android:style/TextAppearance.Widget.EditText">
    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/blue</item> <!-- APPLIED if appearance is part of the theme -->
</style>

<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
    <item name="colorControlNormal">@color...</item>
    <item name="colorControlActivated">@color...</item>
    <item name="editTextStyle">@style/EditTextAppearance</item>
</style>

This way textColor contained in textAppearance is finally applied!

Thicken answered 19/7, 2017 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.