I am making a custom keyboard and have to set composing text before committing it. This is described in this Q&A.
I know how to commit text in general
inputConnection.commitText("text", 1);
But I don't know how to commit it if the user changes the cursor location by touching another part of the EditText
. From observing other keyboards I know it is possible because they do it. But in my keyboard if I have
inputConnection.setComposingText("text", 1);
and then change the cursor position, the composing span is left. Any future changes will replace the composing span, not be entered at the new cursor position.
The post Android EditText listener for cursor position change gives some ideas for what you could do to an EditText
, but inside the custom keyboard I don't have access to the EditText
except for what the InputConnection
gives me.
How do I handle know when the cursor/selection has moved?
I keep finding the answer to my question after I have started writing it. I will post the answer below.
The editor (
EditText
, etc.) callsupdateSelection
on theInputMethodManager
, which in turn notifies theonUpdateSelection
listener. Thus, the keyboard can overrideonUpdateSelection
and take care of the unfinished composing span there.To handle an unfinished composing span you can use
finishComposingText
on theInputConnection
. This will remove the composing span and commit whatever text was in the span.Here is how it is implemented in the sample Android soft keyboard: