How to make text grey in TextInputEditText after TextInputLayout enabled = false?

Viewed 543

When I disable my TextInputLayout

InputTextContainer.isEnabled = false

it's getting grey so visually you understand that it's unavailable but text in it still black even it's blocked too. I want my text to be grey as the disabled container. Can you help me to do that?

3 Answers

Using the default theme, the text in a TextInputLayout's contained EditText is also grayed out when you disable a TextInputLayout. You may have done something in your theme or specific styling on your EditText that has overridden this.

For example, if you have specified a text color like this:

<com.google.android.material.textfield.TextInputEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    tools:hint="Hint"
    tools:text="Text" />

...you have specified a single color that cannot react to different states of the view. Here I defined black #000000, so the text will be black no matter what.

If you want a color that turns gray or transparent when it's disabled, you need to define a ColorStateList color in XML and use that as your android:textColor. Make sure the ColorStateList has at least one state that corresponds with state_enabled="false". For example:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:color="#80000000"
        android:state_enabled="false"/> <!-- Translucent black for disabled state -->
    <item
        android:color="#FF000000"/> <!-- Default -->
</selector>

Suppose this is your xml view

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/pswrd"
        .....>

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/pswrd_text"
            ....>

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

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

you can try 2 things:

  1. along with setting pswrd.isEnabled = false, set pswrdText.isEnabled=false as well.
  2. in case 1 doesnt work, try pswrdText.setTextColor(android.R.color.darker_gray)

Try this method just as @Nitin Verma answered.

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/edt_email">

<TextView
  android:id="@+id/textView20"
  android:layout_width="wrap_content"/>

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

edt_email.isEnable = false
textView20.isEnable = false
Related