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?
In your xml code set focusable="false" , android:clickable="false" and android:cursorVisible="false" and this will make your EditText treat like non editable.
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.
Set attribute android:enable="false"
in TextInputEditText
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With