3

I am implementing form using textinputlayout in Android

I don't want to change hint text color while setting error on textinputlayout as per below textinput layout with error enabled

textInputLayout.setErrorEnabled(true)
textInputLayout.setError("this field is required ")

// As of now this code is changing error message and hint color to red. But I don't want to change hint color to red. Only message color should be changed to red

I want to change error hint "nickname" to blue and error message to red color.

Luke
  • 2,539
  • 2
  • 23
  • 40
ultimatedeep
  • 185
  • 1
  • 2
  • 9

5 Answers5

2

One solution:

Use helper text styled as error text instead of error text.

<com.google.android.material.textfield.TextInputLayout
    style="@style/TextAppearance.TextInputLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:errorEnabled="false"
    app:helperTextEnabled="true"
    app:helperText="Test error message"
    app:hint="your hint">
    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>

and made style that corresponds to your error style.

Milan Jurkulak
  • 557
  • 3
  • 6
  • This is a good approch! If you need to take care of the Container to be read, like me, I just add a extra view to show the desirde red line. it's silly but working. – Mia Jun 02 '23 at 03:47
0

TLDR; It seems there's no way or workaround to do that.

Setting hint color to be different from error seems to be not possible to achieve with xml styling. This and this didn't work. No matter what combination I used, hint was always changed when error was set (to the error color).

So I did a bit of digging in the TextInputLayout code. It has hint color saved defaultHintTextColor, but this one is not used when state changes:

private void updateLabelState(boolean animate, boolean force) {
...
 } else if (errorShouldBeShown) { 
 collapsingTextHelper.setCollapsedTextColor(indicatorViewController.getErrorViewTextColors());
...

The collapsingTextHelper seems to take responsibility for the hint drawing (it's the only one using hint color). However none on the properties I set after error happened worked hintTextColor, setHintTextAppearance, defaultHintTextColor (tried with postDelayed). In fact setting any of these can trigger collapsingTextHelper.setCollapsedTextColor(hintTextColor); but then there's always update that triggers above code:

      if (defaultHintTextColor == null) {
        collapsingTextHelper.setCollapsedTextColor(hintTextColor);
      }

      focusedTextColor = hintTextColor;

      if (editText != null) {
        updateLabelState(false);
      }

If you debug or check layout inspector you will not see a corresponding text view for the hint. The text is drawn by collapsingTextHelperso there's no even a way to get the hint view sadly.

Luke
  • 2,539
  • 2
  • 23
  • 40
0

By default, when an error is set on a TextInputLayout, the hint textColor is changed to the error color. To prevent this behavior, you can use a custom style for the TextInputLayout that overrides the hint textColor and the error textColor. It's a step-by-step process, follow me:

First step:

Create a new style in your styles.xml file that extends the default Widget.MaterialComponents.TextInputLayout.FilledBox style:

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

Second step:

Apply the custom style to your TextInputLayout in the layout file. Here's an example:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/my_text_input_layout"
    style="@style/MyTextInputLayoutStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="My Hint Text">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/my_text_input_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

Third step:

In your code, when you want to set an error on the TextInputLayout, use the setError() function (and not .error) like this:

myTextInputLayout.setError("My Error Message", false)

Note that the second parameter in the setError() function is set to false. This way, the TextInputLayout doesn't change the hint textColor when an error is set. Instead, the error text color will be used for the error message.

halfer
  • 19,824
  • 17
  • 99
  • 186
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
-1

Set To Main Theme of App, It Works Only Highlight State Only

 <item name="colorAccent">@color/Color Name</item>
Nensi Kasundra
  • 1,980
  • 6
  • 21
  • 34
-2

Try The Below Code It Works In Normal State

Change the theme of your TextInputLayout as given below

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ti_email"
        android:hint="@string/EmailAddress"
        android:layout_toLeftOf="@+id/imEntremail"
        android:textColorHint="@color/view"
        app:hintTextAppearance="@style/HintTextStyle">

        <EditText
            android:id="@+id/etentrEmail"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_30sdp"
            android:layout_marginTop="@dimen/_10sdp"
android:cursorVisible="false"
            android:imeOptions="actionNext"
android:padding="@dimen/_3sdp"
            android:gravity="left|bottom"
            android:textColorHint="#B8B8B8"
            android:textSize="@dimen/_11ssp"
            android:maxLines="1"
            android:inputType="text"
            android:background="@android:color/transparent"
            android:textColor="#000000"/>
    </com.google.android.material.textfield.TextInputLayout>

In Styles Folder HintTextStyle Code change the android:textColor code to change your floating label color

<style name="HintTextStyle" parent="TextAppearance.Design.Hint">
    <item name="android:textSize">14sp</item>
    <item name="android:padding">26dp</item>
    <item name="android:textColor">#ff0000</item>
</style>
Apps Maven
  • 1,314
  • 1
  • 4
  • 17