0

Like the hundreds questions out there I want to change na color of the hint. Originally i wanted to ask in the comments of another such question(unanswered) but I don't have enough reputation so I ask here.

I want when the hint is in the TextInputEditText to be one color, and when it is floating to be another. In the other posts the answers are always how to change the hint when the view is focused. That is not what i want, I can do that. I want to change the hint color if the Edit Text is not empty. I'm using material design textInputLayout and textInputEditText. Tried different styles and selectors and still can't do it. Tried to extend TextInputLayout but i don't know how to bind it to EditText so that i can use OnTextListener for the child view.

My code so far can change the color of the hint only when the view is focused.

Styles:

<style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
    <item name="colorControlNormal">@color/green_03</item>
    <item name="colorControlActivated">@color/green_03</item>
    <item name="hintTextColor">@color/dark_blue_60</item>
</style>

<style name="MyHintStyle" parent="@android:style/TextAppearance">
    <!-- Hint color when floating and focused-->
    <item name="android:textColor">@color/dark_blue_60</item>
    <!-- doesn't change anything -->
    <item name="android:textColorHint">@color/dark_blue</item>
    <item name="colorAccent">@color/green_03</item>

    <item name="colorControlNormal">@color/green_03</item>
    <item name="colorControlActivated">@color/green_03</item>

</style>

XML:

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:textColorHint="@color/dark_blue"
            android:theme="@style/TextInputLayoutAppearance"
            app:errorEnabled="true"
            app:hintTextAppearance="@style/MyHintStyle">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/textInputEditText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:backgroundTint="@color/green_03"
                android:hint="@string/phoneNumber"
                android:textColor="@color/dark_blue"
                android:inputType="number"
                android:maxLength="3"
                android:text="12" />

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

Is there a way to extend and change it programmatically for all views or set a global listener or a right way to use styles and selectors.

PleaseHelp
  • 53
  • 1
  • 7

1 Answers1

0

I think you could check if editext contains something or if is is focused. If is so - then you change color of hint. Best place to check this - View.FocusListener of EditText.

Yrii Borodkin
  • 772
  • 5
  • 7
  • Tried it. I extended the edit text and aded a focus listener but you cannot change the color of the floating hint from the edit text. So I wanted to get the parent view of the edit text, but to no success. If you know how to do that tell me pls – PleaseHelp Feb 21 '20 at 13:51
  • `setHintTextAppearance` in TextInputLayout is what you need. You can pass there style resource for styling your hint – Yrii Borodkin Feb 21 '20 at 14:05
  • or `setHelperTextColor` for shortcut - https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout?hl=en#xml-attributes – Yrii Borodkin Feb 21 '20 at 14:07
  • `setHelperTextColor` is not for hints, What style do I pass to `hintTextAppearance`. I tried passing state_active, state_focused, state_normal, state_enabled - didn't work – PleaseHelp Feb 21 '20 at 14:15