0

I have some View which fills entire layout. Activity listen keyboard events in dispatchKeyEvent and do something with this view (immediately, once for each character). But if any kind of autocompletion or auto-spellchecking or whatever is ON - activity will not receive anything until user commit his input. That is unacceptable.

There is solution for EditText.

What can i do to force activity/view act like EditText with input type textFilter (or textNoSuggestions, or 'textVisiblePassword)?

P.S. i can place hidden EditText somewhere in my layout and most likely it will work acceptable. but here could be some side effects and the solution is not beautiful at all.

Community
  • 1
  • 1
mjollneer
  • 1,005
  • 2
  • 19
  • 36
  • "i have some view" - what view, some code if cusrom one? – pskink Feb 27 '14 at 14:26
  • That does not matter at all. It could be 'TextView' with log of all keys pressed or 'GLSurfaceView' runing the game with keyboard input. The important thing is that the screen is filled with something entirely and activity handle key events. – mjollneer Feb 27 '14 at 16:09
  • 1
    what does your view returns in onCreateInputConnection ? – pskink Feb 27 '14 at 17:21
  • Never heard about this. But I've just read spec and it looks very promising! Can you give an example? – mjollneer Feb 27 '14 at 18:22
  • don't have any, see TextView.java for a default implementation – pskink Feb 27 '14 at 19:01

1 Answers1

0

Finally solved.

Thanks to pskink for key clue. Important link.

The fact is that keyboard need to get someеthing called EditorInfo from view, which is in focus. It allows EditText with certain inputMethod works properly.

So we just need to copy that behavior. You can use any view. I have override LinearLayout, use it as root viewGroup in xml and mark all views as

android:focusable="false"
android:focusableInTouchMode="false"

and only root has those tags "true". Not sure if both tags important.

then you need just to override two methods in your view

public boolean onCheckIsTextEditor() {
    return true;
}

public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    outAttrs.inputType = InputType.TYPE_CLASS_TEXT |
        InputType.TYPE_TEXT_VARIATION_PASSWORD |
        InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD |
        InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS |
        InputType.TYPE_TEXT_FLAG_MULTI_LINE;
    outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION;

    return new super.onCreateInputConnection(outAttrs)
}

This will solve most problems. There also some improperly implemented keyboards (for example Emoji Keyboard (at 03/03/14)) which ignores EditorInfo. If you want - you can overcome even this with custom InputConnection, which extends BaseInputConnection - just override

public boolean setComposingText(CharSequence text, int newCursorPosition)

and you will get callback on each composing-event. Not really comfortable, but works at least. And this custom InputConnection must be returned from view's

public InputConnection onCreateInputConnection(EditorInfo outAttrs)

UPD1. Custom InputConnection also usefull with Samsung keyboards.

mjollneer
  • 1,005
  • 2
  • 19
  • 36