如何设置 TextInputLayout 错误信息颜色?

如何改变错误消息的颜色,可以设置为出现在下面的文本字段中的 TextInputLayout(通过 setError(...)-在这里查看错误状态) ?

它通常显示为一个红色的颜色,我想改变。我应该在 styles.xml文件中使用哪个项目名称/键来定位颜色?

先谢谢你。


编辑:

在我的 TextInputLayout增加了 app:errorTextAppearance键:

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="@+id/welcome_current_week_container"
app:errorTextAppearance="@style/WelcomeErrorAppearance">
<EditText
..../>
</android.support.design.widget.TextInputLayout>
</LinearLayout>

和错误外观 (测试时设为绿色):

<style name="WelcomeErrorAppearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@android:color/holo_green_dark</item>
</style>

结果是,提示和错误消息都被着色为 (来自缩放 Android 仿真器的截图):

常规(无错误) :

Before Image

错误状态:

After Image

编辑2/结果:

当出现错误消息时,字段上方的提示将变为与错误消息相同的颜色,覆盖提示颜色-这是设计好的。

76528 次浏览

Create a custom style which uses @android:style/TextAppearance as parent in your styles.xml file:

<style name="error_appearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/red_500</item>
<item name="android:textSize">12sp</item>
</style>

And use it in your TextInputLayout widget:

 <android.support.design.widget.TextInputLayout
android:id="@+id/emailInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/error_appearance">

error example

Edit: Set the hint on the object, which is inside your TextInputLayout (EditText, TextView, etc.) to hold different colors for the hint and the error.

I needed to do this dynamically. Using reflection:

public static void setErrorTextColor(TextInputLayout textInputLayout, int color) {
try {
Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
fErrorView.setAccessible(true);
TextView mErrorView = (TextView) fErrorView.get(textInputLayout);
Field fCurTextColor = TextView.class.getDeclaredField("mCurTextColor");
fCurTextColor.setAccessible(true);
fCurTextColor.set(mErrorView, color);
} catch (Exception e) {
e.printStackTrace();
}
}

You will need to call textInputLayout.setErrorEnabled(true) before invoking the above method for this to work.

UPDATE

Please use a custom view instead and not this


A modded version of @jared's Answer which works in my case :

public static void setErrorTextColor(TextInputLayout textInputLayout, int color) {
try {
Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
fErrorView.setAccessible(true);
TextView mErrorView = (TextView)fErrorView.get(textInputLayout);
mErrorView.setTextColor(color);
mErrorView.requestLayout();
} catch (Exception e) {
e.printStackTrace();
}
}

Actually, to change just the error message color, you can set textColorError in your theme (and also set colorControlNormal and colorControlActivated for the general widget and hint text color). TextInputLayout picks up that attribute. NOTE: if you set errorTextAppearance to a custom style then textColorError will have no effect.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/control_normal</item>
<item name="colorControlActivated">@color/control_activated</item>
<item name="textColorError">@color/error</item>
<!-- other styles... -->
</style>

And in your AndroidManifest.xml:

<application
android:theme="@style/AppTheme"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">


<!-- ... -->


</application>

I looked into the TextInputLayout source and I realised that error text color is gotten from colors.xml. Just add this to your colors.xml:

<color name="design_textinput_error_color_light" tools:override="true">your hex color</color>

One side note. I have tried the accepted solution one with errorTextAppereance. It works really good but at first, the input underline color was not changing after applying a new errorTextAppereance style. I see there are a few comments and that other people are experiencing the same issue.

In my case, this was happening when I was setting a new style after setting a new error text. Like this:

passwordInputLayout.error = "Password strength"
passwordInputLayout.setErrorTextAppearance(R.style.InputError_Purple)

After switching the order of this two methods the text and underline color changes as expected.

passwordInputLayout.setErrorTextAppearance(R.style.InputError_Purple)
passwordInputLayout.error = "Password strength"

And the error text appearance style looks something like this:

<style name="InputError" parent="TextAppearance.Design.Error"/>
<style name="InputError.Purple">
<item name="android:textColor">@color/purple</item>
</style>

If you are using com.google.android.material.textfield.TextInputLayout this input layout than you just set one style

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayoutPassword"
style="@style/LoginTextInputLayoutStyle"






<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">@color/text_input_box</item>
<item name="errorTextColor">@color/colorRed</item>
</style>

Depending on need, one can change/set TextInputLayout text color dynamically or directly in the layout XML file. Below is sample code snippets

Create a custom style which uses @android:style/TextAppearance as parent in your styles.xml file:

<style name="style_error_appearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/color_error</item>
<item name="android:textSize">11sp</item>
</style>

And, use it in your TextInputLayout widget:

  1. Directly in XML Layout
 <android.support.design.widget.TextInputLayout
android:id="@+id/your_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/style_error_appearance">
  1. Dynamically in your class
your_input_layout.setErrorTextAppearance(R.style.style_error_appearance);

If you want to set single/same error text color for your application then define the text color in your app theme

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Error text color... -->
<item name="textColorError">@color/color_error</item>
<!-- other styles... -->
</style>

And in your AndroidManifest.xml:

<application
android:theme="@style/AppTheme"
android:icon="@drawable/ic_launcher"
android:label="@string/your_app_name">


<!-- ... -->


</application>

With the TextInputLayout included in the Material Components Library just use the app:errorTextColor attribute.

    <com.google.android.material.textfield.TextInputLayout
app:errorTextColor="@color/...."
.../>

In a custom style you can use:

<style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox" >
<item name="errorTextColor">@color/...</item>
...
</style>

enter image description here