2

If I create a simple app with single Activity that contains a single EditText, and I do

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

then this prevents the keyboard from appearing (in my tests on Android 5.0 and 7.1). This is what I want, as requested in these questions:

The source code is

public void setTextIsSelectable(boolean selectable) {
    if (!selectable && mEditor == null) return; // false is default value with no edit data

    createEditorIfNeeded();
    if (mEditor.mTextIsSelectable == selectable) return;

    mEditor.mTextIsSelectable = selectable;
    setFocusableInTouchMode(selectable);
    setFocusable(selectable);
    setClickable(selectable);
    setLongClickable(selectable);

    // mInputType should already be EditorInfo.TYPE_NULL and mInput should be null

    setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null);
    setText(mText, selectable ? BufferType.SPANNABLE : BufferType.NORMAL);

    // Called by setText above, but safer in case of future code changes
    mEditor.prepareCursorControllers();
}

But I don't see what about this disables the Input Method from appearing.

Calling the following methods all return true whether I set setTextIsSelectable or not.

editText.isFocusable();             // true
editText.isFocusableInTouchMode();  // true
editText.isClickable();             // true
editText.isLongClickable();         // true

I'm partly asking because I'm curious, but also because I need to disable the system keyboard in my app. I want to understand what is happening so that I can be sure that it is really doing what I think it is doing.

Update

Follow-up question and answer:

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • [Editor.startSelectionActionModeInternal](https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/Editor.java#L2009) – adneal Jul 21 '17 at 03:11
  • 1
    @adneal, So it appears to be because of `!mTextView.isTextSelectable()`. Feel free to add this as an answer. I'm a little curios why making sure that text is not selectable would be a determining factor in showing the keyboard (but that information is not necessary to answer my question). – Suragch Jul 21 '17 at 03:37

1 Answers1

2

The keyboard isn't shown when you call TextView.setTextIsSelectable(true) and then select text as a result of Editor.startSelectionActionModeInternal, which checks if TextView.isTextSelectable is false before showing the text selection ActionMode.

    final boolean selectionStarted = mTextActionMode != null;
    if (selectionStarted && !mTextView.isTextSelectable() && mShowSoftInputOnFocus) {
        // Show the IME to be able to replace text, except when selecting non editable text.
        final InputMethodManager imm = InputMethodManager.peekInstance();
        if (imm != null) {
            imm.showSoftInput(mTextView, 0, null);
        }
    }

My guess to why it's implemented this way is probably because the framework team decided they wanted text Editor could predict to be selectable to not jump around the screen as a result of the keyboard moving it around. It was probably just a UI decision. According to git it's been that way since 2012, but the commit doesn't mention anything specifically regarding that implementation.

adneal
  • 30,484
  • 10
  • 122
  • 151