Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Make TextInputEditText Read-only?

I'm trying to make TextInputEditText read-only but I'm getting a quick flash of cursor and click is triggered.

I tried setting isFocusable = false and isClickable = false.

XML Layout:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/label_address"
    android:layout_marginTop="@dimen/material_layout_vertical_spacing_between_content_areas"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/input_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionNext"/>

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

Code to make read-only:

fun TextInputEditText.disable() {
    isFocusable = false
    isClickable = false
}

What is the correct way or proper method in making TextInputEditText read-only?

like image 609
Philip Borbon Avatar asked Apr 11 '19 07:04

Philip Borbon


People also ask

How do you make Textinputlayout not editable?

In your xml code set focusable="false" , android:clickable="false" and android:cursorVisible="false" and this will make your EditText treat like non editable.

How do I disable Textinputedittext?

To disable an EditText while keeping this properties, just use UI. setReadOnly(myEditText, true) from this library. If you want to replicate this behaviour without the library, check out the source code for this small method.


1 Answers

In XML

Set attribute android:enable="false"in TextInputEditText

The Code

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Hints"
    android:layout_marginTop="10dp"
    android:layout_margin="40dp">
    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/input_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:imeOptions="actionNext"
        android:text="Hi How are you"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary" />
</com.google.android.material.textfield.TextInputLayout>

Result:

enter image description here

like image 119
Rohit Singh Avatar answered Oct 02 '22 21:10

Rohit Singh