I want to make a read-only EditText
view. The XML to do this code seems to be android:editable=\"false\"
, but I want to do this in code.
H
As android:editable=""
is deprecated,
Setting
android:clickable="false"
android:focusable="false"
android:inputType="none"
android:cursorVisible="false"
will make it "read-only".
However, users will still be able to paste into the field or perform any other long click actions. To disable this, simply override onLongClickListener().
In Kotlin:
myEditText.setOnLongClickListener { true }
suffices.
android:editable
If set, specifies that this TextView
has an input method. It will be a textual one unless it has otherwise been specified. For TextView
, this is false by default. For EditText
, it is true by default.
Must be a boolean
value, either true
or false
.
This may also be a reference to a resource (in the form @[package:]type:name
) or theme attribute (in the form ?[package:][type:]name
) containing a value of this type.
This corresponds to the global attribute resource symbol editable.
Related Methods
The best is by using TextView
instead.
writing this two line is more than enough for your work.
yourEditText.setKeyListener(null);
yourEditText.setEnabled(false);