1

When my app starts, I loaded some text inside EditText which cannot be edited by the user, until click 'menu' and select 'edit text'. The initial xml looks like the below:

<EditText
    android:id="@+id/myTextBox"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:cursorVisible="true"
    android:gravity="top"
    android:inputType="none"
    android:textIsSelectable="true"
    android:scrollbars="vertical"
    android:maxLines="1"
     />

After user selects 'edit text', how to I make it editable again? I tried doing the below in code:

_mytextBox.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
_mytextBox.setTextIsSelectable(false);

But the keyboard never shows up again.. If I remove textIsSelectable from xml and use android:inputType="textMultiLine", it works just like I wanted.

Additional Info: Sorry forgot to mention, I cannot use the disable/enable way because I would like the user to be able to select any text inside and do a 'copy'. Also, disabling it will dim the text.. which is not what I want. Thanks!

I would have expect that calling _mytextBox.setTextIsSelectable(false); would reset everything to normal, but it doesn't.. seems like a bug? Might be related to this issue: https://code.google.com/p/android/issues/detail?id=27609

Bruce
  • 2,357
  • 5
  • 29
  • 50

4 Answers4

2

to reset setTextIsSelectable you just have to get a set of EditText properties back to their original states...

this is a Utils method i develop to use in my apps..

public static void toggleSelectableState(EditText editText){
    if(editText.isTextSelectable()){
        editText.setTextIsSelectable(false);
        editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
        editText.setCursorVisible(true);
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.setClickable(true);
        editText.setLongClickable(true);
    }else{
        editText.setTextIsSelectable(true);
        editText.setCursorVisible(false);
    }
}

you are welcome...

  • you saved my day man, my problem was in a recycler view... the recycled view (edit text) sometimes I set isFocusable = false, and i was just trying to set isFocusable = true but was not working, so I have added isFocusableInTouchMode as well and that solved the problem, so seems that if you set focusable = false the focusableInTouchMode is set to false as well... btw thanks a lot – Gustavo Ross Apr 06 '21 at 18:31
1

For enabling a EditText use

_mytextBox.setEnabled(true);

and for Disabling a EditText use

_mytextBox.setEnabled(false);

If you do not want to use above try hiding/showing keyboard on edittext

       // Show soft keyboard for the user to enter the value.
       InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
       imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);




        // Hide soft keyboard.
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
0

Make your EditText initially android:enabled="false" in your xml

And on items onOptionsItemSelected edit text option make it setEnabled(true)

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.edit_text_option_id:
            editText.setEnabled(true);
                return true;
        default:
            return super.onOptionsItemSelected(item);

        }

    }
Manishika
  • 5,478
  • 2
  • 22
  • 28
0

Have you tried requestFocus to editText?

Devrim
  • 15,345
  • 4
  • 66
  • 74
  • Yes, doesn't solve the issue. Cursor and keyboard never show up even after calling _mytextBox.setTextIsSelectable(false); – Bruce Oct 20 '13 at 15:08
  • I guess you've already checked this link http://stackoverflow.com/questions/7200281/programatically-hide-show-android-soft-keyboard – Devrim Oct 20 '13 at 15:11