Android - EditText not editable and not selectable

10,049

Solution 1

Sorry guys, at least for my setup (Sdk Version 25 with AppCompatActivity)

editText.setFocusable(false);

will still let me push the return button of the virtual keyboard, adding lines to the text. If you add

editText.setEnabled(false);

this behaviour stopps, however, the text is greyed out.

Therefore, I think the following would be a better solution:

editText.setInputType(InputType.TYPE_NULL);

Solution 2

This works fine; just set focusable property of your edittext to "false" and you are done.

<EditText
        android:id="@+id/EditTextInput"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:gravity="right"
        android:cursorVisible="true">
    </EditText>

Solution 3

I would implement the following code:

editText.setFocusable(false);
editText.setClickable(false);
editText.setEnabled(false);

Which should prevent interacting with the EditText. Read more about the View class here since EditText extends from it:

https://developer.android.com/reference/android/view/View.html#setFocusable(boolean)

Share:
10,049
Vin
Author by

Vin

Updated on June 04, 2022

Comments

  • Vin
    Vin almost 2 years

    In my activity code I have these lines

    EditText editText=(EditText)findViewById(R.id.editText);
    editText.setTextIsSelectable(false);
    

    What I want to achieve is a EditText uneditable and unselectable. With the previous line I try to achieve the second option, but it'doesn't work. The EditText is still selectable.

    In order to make the EditText uneditable I have not found any method such as setEditable or similar. I read about setting InputType but I had no success.

    However the unselectable constraint can be weakened. My main goal is that the text inside the EditText cannot change for any reason. For these reason the EditRext could be selectable but I want that no user can cut the text.

    I want no XML but only Java code. All these things are needed to to programmatically.

  • Vin
    Vin over 6 years
    setEnabled(false) disable the EditText. It's a drastic solution.
  • Vin
    Vin over 6 years
    It doesn't matter. setFocusable(false) is the way.
  • Vin
    Vin over 6 years
    setClickable(false) seems to do no difference. setFocusable(false) is the right way.
  • gi097
    gi097 over 6 years
    @Vin only using setFocusable() does not disable it, a user can still paste data to the EditText.
  • Vin
    Vin over 6 years
    I didn't think it. Thank you.
  • Vin
    Vin over 6 years
    Wait wait wait. setFocusable(false) is not enough. Because text can be pasted into theEditText, and this is not a good thing.
  • Vin
    Vin over 6 years
    Instead to use setEnabled(false). I used setFocusable(false), setClickable(false) and setLongClickable(false). This seems to prevent text pasting inside the EditText
  • Vin
    Vin over 6 years
    @KalaBalik It doesn't matter means that the solution is understandable. In this case android:focusable="false" is exactly setFocusable(false). Sometimes XML is not fully translatable to Java code, in that case I will not accepted the question.
  • Vin
    Vin over 6 years
    The magic number 0 in the setInputType() method, what does it mean?
  • kalabalik
    kalabalik over 6 years
    The docs say: >"There is no content type. The text is not editable."
  • Vin
    Vin over 6 years
    I tried. The only input type seems to make the EditText not clickable and not long-clickable. I don't know if setClickable(false) and setLongClickable(false) are unnecessary now.
  • kalabalik
    kalabalik over 6 years
    Actually, I can't reproduce what @Vin said earlier about the click and the long click. If no one intervenes soon, I will take out lines 2 and 3 from my answer. Lines 1 and 4 suffice.
  • Vin
    Vin over 6 years
    It seems that with only line 4 (setInputType(0)) the EditText is not editable and not selectable.
  • Vin
    Vin over 6 years
    setFocusable(false) is only if anyone would that all EditTexts are not highlited when are clicked.
  • lucidbrot
    lucidbrot about 5 years
    Perfect for making an EditText that is actually not editable. This answer also avoids the keyboard showing.