Can't get token to change soft keyboard

  • Replies:0
Sapientum
  • Forum posts: 1

Nov 26, 2015, 12:58:14 PM via Website

I have a custom keyboard, where I have a button to change IME....

From the Android sample code, I've found the following:

InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imeManager.switchToNextInputMethod(getToken(), false /* onlyCurrentIme */);

But I'm just getting can't resolve 'getToken()'... I'm doing this from a class that extends InputMethodService... What's the correct approach to getting the token from inside a keyboard then?

If not the above, then this one could work as well:

imeManager.showInputMethodPicker();
imeManager.hideSoftInputFromWindow(/*token?*/, 0);

My class:

public class CustomKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

    private KeyboardView kv;
    private Keyboard keyboard;

    public final static int CodeRowFourGlobe = 55041;

    @Override
    public View onCreateInputView() {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.custom_keyboard);
        kv.setPreviewEnabled(false);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    public void closeKeyboard(View view) {
        InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imeManager.switchToNextInputMethod(view.getWindowToken(), false /* onlyCurrentIme */);
    }

    @Override
    public void onPress(int primaryCode) {

    }

    @Override
    public void onRelease(int primaryCode) {

    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();

        if (primaryCode == Keyboard.KEYCODE_DELETE) {
            ic.deleteSurroundingText(1, 0);
        } else if (primaryCode == CodeRowFourGlobe) {
            InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imeManager != null) {
                Log.d(TAG, "Close keyboard");

            } else {
                Toast.makeText(this, R.string.no_keyboard_available, Toast.LENGTH_LONG).show();
            }
        } else {

        }
    }
}

I've tried:

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(kv.getApplicationWindowToken(), 0);
imm.switchToNextInputMethod(kv.getApplicationWindowToken(), false);
imm.switchToNextInputMethod(kv.getWindowToken(), false /* onlyCurrentIme */);
imm.hideSoftInputFromWindow(kv.getWindowToken(), 0);

Reply