Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change material TextInputLayout hint color on empty edittext?

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?

like image 592
Amin Avatar asked Nov 06 '22 23:11

Amin


1 Answers

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)
        }
like image 60
Mr.King Avatar answered Nov 14 '22 06:11

Mr.King