Question

In my soft keyboard I can perfectly capture the event key for deleting as below

public void onKey(int primaryCode, int[] keyCodes) {
    if (primaryCode == Keyboard.KEYCODE_DELETE) {
        int ccLength = composingContent.length();
        if (ccLength > 1) {
            composingContent.delete(ccLength - 1, ccLength);
            getCurrentInputConnection().setComposingText(composingContent, ccLength - 1);
        }
    }
}

The problem is when the new composingContent is set, it is appended to the end of the old text. It will not clear the old content. I also used

getCurrentInputConnection().commitText(composingContent, ccLength - 1);

But this also does not clear any composing text set previously

Was it helpful?

Solution

I found a way. This approach solved my problem but I think there should be a better way. I'm posting this in case anyone is interested. What I did was before calling setComposingText, first, I called setSelection. By doing this I'm selecting all the text in my input then I'm setting the composing text.

InputConnection conn = getCurrentInputConnection();
    if (conn != null) {
        int ccLength = composingContent.length();
        if (ccLength > 1) {
            composingContent.delete(ccLength - 1, ccLength);
            conn.setSelection(0, ccLength);
            conn.setComposingText(composingContent, 1);
        } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top