'Making textinputlayout uneditable and setting click event on it

I want to make textinputlayout uneditable by user and when user click on it i want to perform an action. I added android:focusable="false" inside edittext of textinputlayout to make it uneditable but now when i click on it it is not getting callback inside onclicklistener of textinputlayout.

   <android.support.design.widget.TextInputLayout
        android:id="@+id/tipVideoFilePath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/uploadVideo"
        android:layout_toLeftOf="@+id/uploadVideo">

    <EditText
        android:id="@+id/etVideoFilePath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/dp5"
        android:singleLine="true"
        android:focusable="false"
        android:hint="@string/videoPath"
        android:textColor="@color/black"/>
</android.support.design.widget.TextInputLayout>


Solution 1:[1]

I had the same issue for TextInputLayout and TextInputEditText. I wanted to open a dialog. Changing the TextInputEditText's "clickable" property to true worked for me

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/country_chooser_layout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp">
    
    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/country_chooser"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:drawableEnd="@drawable/ic_expand_arrow"
        android:focusable="false"
        android:hint="Country"
        android:inputType="textCapWords" />
</com.google.android.material.textfield.TextInputLayout>

Kotlin :

val countryChooser = view.findViewById<TextInputEditText>(R.id.country_chooser)
countryChooser.setOnClickListener { v -> showCountryDialog(v) }

Solution 2:[2]

Try this code. get on clck listener for the edittext.

<android.support.design.widget.TextInputLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
        <EditText
            android:id="+id/etVideoFilePat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:clickable="true" />
</android.support.design.widget.TextInputLayout>

Solution 3:[3]

  1. Set TextInputLayout android:clickable="true".

  2. Set TextInputEditText android:clickable="true" and android:focusable="false".

  3. Set View.OnClickListener on both TextInputLayout and TextInputEditText.

    <com.google.android.material.textfield.TextInputLayout
         android:id="@+id/text_input_layout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:clickable="true">
    
         <com.google.android.material.textfield.TextInputEditText
             android:id="@+id/edit_text"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:clickable="true"
             android:focusable="false"/>
    </com.google.android.material.textfield.TextInputLayout>
    

Note: It's important to set View.OnClickListener on both views because TextInputLayout bigger than and behind TextInputEditText.

Solution 4:[4]

You can try these

edittext.setFocusable(false);
edittext.setClickable(true);

Solution 5:[5]

Or this:

put EditText in another Layout (RaltiveLayout) and make EditText Match_Parent

edittext.setFocusable(false);
edittext.setClickable(false);

relativelayout.setFocusable(true);
relativelayout.setClickable(true);

apply the onclicklistener on relativelayout

Solution 6:[6]

My layout:

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tvSelectBankInputLayout"
                style="@style/MyEditText.TextInputLayout.FilledBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_16"
                android:clickable="true"
                android:hint="@string/txt_title_bank_name"
                app:endIconDrawable="@drawable/ic_right_muiten"
                app:endIconMode="custom"
                app:errorIconDrawable="@null">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/tvSelectBank"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:cursorVisible="false"
                    android:ellipsize="end"
                    android:focusable="false"
                    android:imeOptions="actionNext"
                    android:inputType="none"
                    android:maxLines="1"
                    android:scrollHorizontally="true"
                    android:singleLine="true"
                    android:textColor="@color/c_212121"
                    android:textSize="@dimen/sp_16" />

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

And this is my code:

tvSelectBank?.setOnClickListener {
    startActivityForResult(intentFor<ListBankActivity>(), REQUEST_CODE)
}

Solution 7:[7]

You can also define the inputType of your EditText at none to make it uneditable.

<android.support.design.widget.TextInputLayout
    android:id="@+id/tipVideoFilePath"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/uploadVideo"
    android:layout_toLeftOf="@+id/uploadVideo">

    <EditText
        android:id="@+id/etVideoFilePath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"
        android:paddingLeft="@dimen/dp5"
        android:singleLine="true"
        android:focusable="false"
        android:hint="@string/videoPath"
        android:textColor="@color/black" />

</android.support.design.widget.TextInputLayout>

Solution 8:[8]

This worked for me (in Kotlin): editText.apply { isFocusableInTouchMode = false isClickable = false isLongClickable = false isEnabled = false }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ben Bloodworth
Solution 2
Solution 3
Solution 4 Aytek Sökmen
Solution 5 Martin
Solution 6 סטנלי גרונן
Solution 7 Alex CHOPARD
Solution 8 kassim