Search code examples
androidimesoft-keyboardimeoptions

Change "Done" label on my softkeyboard to "Search,Next" according to EditText ime options


I'm developing a soft keyboard for Android and I want the "DONE" key (KEYCODE_DONE) to be changed to "search, next" according to EditText ime options. How can I achieve that?


Solution

  • Finally I found it. Create a class which extends Keyboard and add the following method and call it from InputMethodService class.

    @Override
    protected Key createKeyFromXml(Resources res, Row parent, int x, int y, XmlResourceParser parser) {
        Key key = new Key(res, parent, x, y, parser);
        if (key.codes[0] == -4) {
            mEnterKey = key;
        }
        return key;
    }
    
    private Key mEnterKey;
    void setImeOptions(Resources res, int options) {
        if (mEnterKey == null) {
            return;
        }
    
        switch (options & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
            case EditorInfo.IME_ACTION_GO:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = "Go";
                break;
            case EditorInfo.IME_ACTION_NEXT:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = "Next";
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                mEnterKey.icon = null;
                mEnterKey.iconPreview = null;
                mEnterKey.label = "Search";
                break;
            case EditorInfo.IME_ACTION_SEND:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = "Send";
                break;
            default:
                mEnterKey.icon = null;
                mEnterKey.iconPreview = null;
                mEnterKey.label = "Return";
                break;
        }
    }