4

I'm trying to change TextInputLayout hint color in case it's edittext empty and not focused but I can't do that, and here is my layout

<com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/input_layout"
                        style="@style/text_material_input_hint_neutral_80_normal"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="17dp"
                        android:layout_marginTop="26dp"
                        android:layout_marginEnd="17dp"
                        android:layout_marginBottom="24dp"
                        android:hint="@string/store_url"
                        android:textColorHint="@color/neutral_80"
                        app:boxBackgroundColor="@color/neutral_10"
                        app:boxCornerRadiusBottomEnd="8dp"
                        app:boxCornerRadiusBottomStart="8dp"
                        app:boxCornerRadiusTopEnd="8dp"
                        app:boxCornerRadiusTopStart="8dp"
                        app:boxStrokeWidth="0dp"
                        app:boxStrokeWidthFocused="0dp"
                        app:hintTextColor="@color/neutral_80"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent">


                        <com.google.android.material.textfield.TextInputEditText
                            android:id="@+id/edittext"
                            style="@style/text_material_input_text_neutral_130_normal"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:text="test string"
                            android:paddingStart="12dp"
                            android:paddingTop="24dp"
                            android:textAlignment="viewStart" />

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

and here is my used styles

  <style name="text_material_input_hint_neutral_80_normal" parent="TextAppearance.AppCompat">
        <item name="android:textSize">12sp</item>
        <item name="android:textAllCaps">false</item>
        <item name="android:includeFontPadding">false</item>
        <item name="android:textColor">@color/neutral_80</item>
    </style>

    <style name="text_material_input_text_neutral_130_normal" parent="TextAppearance.AppCompat">
        <item name="android:textSize">14sp</item>
        <item name="android:colorAccent">@color/secondary_80</item>
        <item name="android:textAllCaps">false</item>
        <item name="android:includeFontPadding">false</item>
        <item name="android:textColor">@color/neutral_130</item>
        <item name="colorControlActivated">@color/secondary_80</item>

    </style>

I'm using colorControlActivated as some answers suggests on similar case and also android:colorAccent but that's not help?

How can I apply a different hint color in case the edit text is empty and not focused?

Junior
  • 1,007
  • 4
  • 16
  • 26
Amin
  • 463
  • 2
  • 11
  • 29

1 Answers1

1

Order of calling function is really important.

binding.txtLoginInputLayout.apply {
            defaultHintTextColor = ContextCompat.getColorStateList(context, R.color.blue)
            hintTextColor = ContextCompat.getColorStateList(context, R.color.red)
        } 

  binding.edtLoginInputLayout.apply {
            setTextColor(
                ContextCompat.getColorStateList(
                    context,
                    R.color.green
                )
            )

            highlightColor = ContextCompat.getColor(context, R.color.green_light)
        }
Mr.King
  • 11
  • 2
  • the same issue it's works only if the user focus on the current input layout edittext and the edittext was empty but if the user write something and focus on other edittext it shows the default hint color rather than the hint color shown on focus @Mr.King – Amin Nov 26 '21 at 23:54