androidandroid-edittextandroid-textinputlayoutmaterial-components-androiddisabled-control

programmatically change TextInputLayout textColorHint color to indicate/show a disabled editText inside


I have a EditText that in inside a TextInputLayout. I would like to change the color of the TextInputLayout's textColorHint programmatically. As I would need to enable and disable the editTexts while user is filling up the forms. For an example, A forget password Form with 5 editText and 3 buttons. User has to submit their email, verification code, new and confirm password. Only when their email is submitted, then the verification code EditText, verification submission button is enabled. and so on.

Hence To show disabled, I would like the EditText text(placeholder) also known as android:hint in EditText xml, underline to be slightly grey out / more transparent. It is possible to resolve this using setAlpha(.5f); but I would like to change the color. Change my textPlaceholderColor(grey) to a lighter grey color.

I have tried the app:hintTextApperance=@style/TextInputLayoutHintText "method" but android:TextColorHint does not seem to work inside. I even tried to change the style using codeLayout.setHintTextAppearance(R.style.TextInputLayoutHintTextDisabled); I have also tried the selector "method" in EditText. But its seem that android:TextColorHint does not work in EditText; setting a color to it do not work. It has to be change inside TextInputLayout.

in my style file

    <!-- Legacy TextField Theme-->
    <style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
        <item name="colorControlNormal">@color/textPlaceholderColor </item>
        <item name="colorControlActivated">@color/textFieldColor</item>
        <item name="colorControlHighlight">@color/textFieldColor</item>
    </style>


    <!-- TextField Hint Theme-->
    <style name="TextInputLayoutHintText">
        <item name="android:textColor">@color/textPlaceholderColor</item>
        <item name="android:textSize">16sp</item>
        <item name="android:textColorHint">@color/disableTextColor </item>
    </style>

    <!-- TextField Text Color Hint Theme Disabled-->
    <style name="TextInputLayoutHintTextDisabled">
        <item name="android:textColor">@color/disableTextColor </item>
        <item name="android:textSize">16sp</item>
        <item name="android:textColorHint">@color/disableTextColor </item>
        <item name="hintTextColor">@color/disableTextColor </item>
    </style>

in my activity xml file

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/forgetTextInputLayout2"
            style="@style/TextInputLayoutAppearance"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="20dp"
            android:elevation="12dp"
            app:endIconMode="clear_text"
            android:textColorHint="@color/textPlaceholderColor"
            app:hintTextAppearance="@style/TextInputLayoutHintText"
            app:layout_constraintEnd_toEndOf="@+id/forgetForm"
            app:layout_constraintStart_toStartOf="@+id/forgetForm"
            app:layout_constraintTop_toBottomOf="@+id/forget_submit_email">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/forgetVerification"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:fontFamily="@font/open_sans"
                android:hint="@string/ForgetForm3"
                android:inputType="textPersonName"
                android:singleLine="true"
                android:textColor="@drawable/edit_text_selector"
                android:textCursorDrawable="@drawable/cursor_color"
                android:textSize="18sp" />
        </com.google.android.material.textfield.TextInputLayout>

color file

  <color name="colorPrimary">#00BEBA</color>
    <color name="colorPrimaryDark">#00BEBA</color>
    <color name="colorAccent">#F60606</color>
    <color name="white">#FFFFFF</color>
    <color name="green">#17B3AB</color>
    <color name="version">#0B0008</color>
    <color name="appBackgroundColor">#00BEBA</color>
    <color name="textPlaceholderColor">#807E7E</color>
    <color name="textFieldColor">#000000</color>
    <color name="textFieldCursorColor">#FB0000</color>
    <color name="textFieldHightLightColor">#2196F3</color>
    <color name="customGreen">#92CFCD</color>
    <color name="defaultLinkBlue">#007aff</color>
    <color name="facebookBlue">#3B5998</color>
    <color name="disableTextColor">#3C95A1A0</color>

Solution

  • You can so it with a custom style:

        <com.google.android.material.textfield.TextInputLayout
            style="@style/MyTextInputLayout"
    

    defining a selector:

      <style name="MyTextInputLayout " parent="Widget.MaterialComponents.TextInputLayout.FilledBox" >    
       <item name="android:textColorHint">@color/my_selector</item>.  
      </style>
    

    with:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:alpha="0.38" android:color="@color/colorSecondaryLight" android:state_enabled="false"/>
        <item android:alpha="0.6" android:color="@color/colorPrimary"/>
    </selector>
    

    In this way enabling/disabling the TextInputLayout the color changes.

    enter image description here

    enter image description here

    Programmatically you can use the setDefaultHintTextColor method:

        TextInputLayout textInputLayout = findViewById(R.id.til);                   
        textInputLayout.setDefaultHintTextColor
          (ContextCompat.getColorStateList(this,R.color.selector));