3

I am making a custom keyboard that I want to include in an app. I already know how to make a system keyboard. I don't want to do that because it requires installing by the user.

Whenever the user presses a key on the keyboard it should send the key text to whichever EditText currently has focus (if any).

The documentation states

An editor needs to interact with the IME, receiving commands through this InputConnection interface, and sending commands through InputMethodManager.

This is illustrated in the following image (where View is an EditText).

enter image description here

This makes it sound like I should be using an input connection to communicate with the EditText. So my question is, how does my custom keyboard view get a reference to the currently focused EditText's input connection. Or how does it start that connection?

Related

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • `View#onCreateInputConnection` ? – pskink Jul 06 '17 at 10:10
  • the docs say: *"Create a new InputConnection for an InputMethod to interact with the view. The default implementation returns null, since it doesn't support input methods. You can override this to implement such support. This is only needed for views that take focus and text input."* – pskink Jul 06 '17 at 10:13
  • @pskink Oh, I read your first comment too quickly. I thought it said something like `getInputConnection`. I have overridden `onCreateInputConnection` to make a custom `EditText` (see [here](https://stackoverflow.com/a/44342915/3681880) and [here](https://github.com/suragch/mongol-library/blob/master/mongol-library/src/main/java/net/studymongolian/mongollibrary/MongolEditText.java)). Now I am trying to go the other way, that is, make the IME that talks to the `EditText`. – Suragch Jul 06 '17 at 10:21
  • So the EditText's `onCreateInputConnection` returns an input connection, but how does my keyboard get a hold of it? – Suragch Jul 06 '17 at 10:24
  • inside `InputMethodManager#startInputInner` – pskink Jul 06 '17 at 10:29
  • @pskink, [this looks complicated](https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/inputmethod/InputMethodManager.java#L1146) but I will study how Android did it. Thanks. – Suragch Jul 06 '17 at 10:36
  • yes, line 1200: `InputConnection ic = view.onCreateInputConnection(tba);` – pskink Jul 06 '17 at 10:39
  • @pskink, I always associate `onDoSomething` methods with listeners or something you override in a subclass. I assumed only the system calls these type of methods directly. It took me a while to realize that I could call it directly myself. You seem to always answer my questions in the comments. If you want to add it as an answer, I will change the accepted answer to yours. Anyway, thanks. – Suragch Jul 20 '17 at 23:42

2 Answers2

7

As @pskink mentioned in the comments, you can use

InputConnection ic = editText.onCreateInputConnection(new EditorInfo());

to get a reference to an EditText's input connection.

It can be handed off to a custom keyboard when the EditText receives focus by adding a listener.

// get the input connection from the currently focused edit text
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            InputConnection ic = editText.onCreateInputConnection(new EditorInfo());
            keyboard.setInputConnection(ic); // custom keyboard method
        }
    }
});
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
0

I think you mean

InputConnection ic = v.onCreateInputConnection(new EditorInfo());
AS Mackay
  • 2,831
  • 9
  • 19
  • 25