2

I've found many questions about setting an edit text to readOnly or to editable, but all of them only work for some specific scenarios. I need the following functionality:

An Edittext widget which is editable should be set to readonly programmatically. When it is readonly, it shouldn't be possible to input data, but the current content should be selectable to copy. The same field should set back to editable to the same state as before, so allow input and select the text for copy/paste.

This is my current code:

        if (readOnly)
        {
            _text.setTextColor(Color.GRAY);
            _keyListener = _text.getKeyListener();
            _text.setKeyListener(null);
        }
        else
        {
            _text.setTextColor(_foreground);
            if (_keyListener != null)
            {
                _text.setKeyListener(_keyListener);
            }
            _text.setFocusable(true);
        }

I already tried the following methods:

  • Call setTextIsSelectable() in the readonly part. This enables selection of text in the readonly field. But if I change it back to editable, the field is still only selectable.
  • Set the inputtype to null for readonly state and set it back to the previous state to set it editable. With this, the the field isn't editable after setting it back to editable.

I think I forgot to set some kind of Listener or property. Maybe one of you knows which one that is.

Edit: Tried the following: Disabling of EditText in android But it only works as long as I won't use the setTextIsSelectable() to make the readonly text selectable. If I set this value and switch back to editable, the field is only selectable, but not editable.

Thanks

Roman
  • 470
  • 2
  • 5
  • 17
  • Possible duplicate of [Disabling of EditText in android](https://stackoverflow.com/questions/4297763/disabling-of-edittext-in-android) – Anton Potapov Aug 29 '17 at 13:55
  • Unfortunately not working, see my Edit. – Roman Aug 29 '17 at 14:03
  • 1
    If nothing else is working you could try using two different widgets, and only show the relevant one. Show a TextView while read only and and EditText while editable, and use TextWatchers to keep them in sync while hidden. – cwbowron Aug 29 '17 at 14:15
  • @cwbowron: Thanks for this possibility. I try to avoid to have multiple widgets per field, but I'll take this solution if there's no other one. – Roman Aug 29 '17 at 14:25

5 Answers5

9

Use this parameters android:textIsSelectable="true" android:inputType="none" (android:editable is deprecated)

or programmatically

editText.setInputType(InputType.TYPE_NULL); editText.setTextIsSelectable(true);

If this is dont work you can try something custom, at first use android:textIsSelectable="true"in your xml, after define two methods

private void disableEditText() {
        editText.setInputType(InputType.TYPE_NULL);
        disabled = true;
}
private void enableEditText() {
        editText.setInputType(InputType.TYPE_CLASS_TEXT);
        disabled = false;
}

and open keyboard every time when text is nor disabled and use click to editText

 editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (!disabled) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
            }
        }
    });

And don't forget to define boolean disabled

Hope this is help you

L.Petrosyan
  • 430
  • 4
  • 12
  • Unfortunately, this is not working. I can set the field disabled with this, but I don't know how to set it do enabled again. – Roman Aug 30 '17 at 08:35
  • This is it almost. I made two changes: Use onFocusListener instead of onClickListener to prevent to have to click twice on the same field to open the keyboard. Second change: Use imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); instead of imm.toggleSoftInput() to prevent closing the keyboard on clicking on another field. Could you do these changes, so I can set this answer as the accepted answer. – Roman Aug 30 '17 at 12:54
  • 1
    I did the changes :))) Thanks .....Although I think `onClickListener` is better solution when u dont have any other items in view , for change your focus :) – L.Petrosyan Aug 30 '17 at 13:38
  • hmm that's right. If you close the keyboard you can't open it again until the focus changes. I think both solutions have (dis-)advantages. Maybe there's a better one, but for now, this works for me. :) – Roman Aug 30 '17 at 13:58
5

bool isEnabled = false

eidtText.Focusable= isEnabled ;
eidtText.FocusableInTouchMode = isEnabled ;
eidtText.Clickable= isEnabled ;
eidtText.LongClickable= isEnabled ;
eidtText.SetCursorVisible(checkedFalse) ;

Working and Tested! this is from xamarin c# but you could convert it in java easily by adding prefix "Set" to some code

Gideon Huyo-a
  • 61
  • 1
  • 2
4

Based on Gideon Huyo-a's Answer this is converted to Java and is usable on Android Studio

  private void EnableDisableEditText(boolean isEnabled, EditText editText) {
    editText.setFocusable(isEnabled);
    editText.setFocusableInTouchMode(isEnabled) ;
    editText.setClickable(isEnabled);
    editText.setLongClickable(isEnabled);
    editText.setCursorVisible(isEnabled) ;
}

Also Converted to Method just use

  EnableDisableEditText(BooleanValueHere, YourEditText)
1

you can enable and disable the edit text which will prevent the editing of the text by

editText.isEnabled = true
editText.isEnabled = false
Coder
  • 33
  • 4
-2

You can use android:enabled="false" in xml file for read only or use:

txtEditetext.isEnabled =false for read only
and txtEditetext.isEnabled = true for edit text

Nimantha
  • 6,405
  • 6
  • 28
  • 69
MRB
  • 1
  • 1