EdJ
EdJ

Reputation: 1306

Programatically change keyboard from custom android keyboard

I have created an android custom keyboard. After pressing a button on it, I'd like it to change the keyboard back to the previous keyboard, presumable using InputMethodManager.setInputMethod(IBinder token, String id);

However, I can't work out where to get the token from - using getCurrentInputBinding().getConnectionToken() doesn't work.

Does anyone know where to find the token?

Thanks,

Ed

Upvotes: 3

Views: 1889

Answers (3)

Mohamed AbdelraZek
Mohamed AbdelraZek

Reputation: 2809

You can use this Method to get Token and activate last used Keyboard

 private fun switchToLastKeyboard() {
        try {
            val imm: InputMethodManager =
                this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            val token = this.window.window!!.attributes.token
            //imm.setInputMethod(token, LATIN);
            imm.switchToLastInputMethod(token)
        } catch (t: Throwable) { // java.lang.NoSuchMethodError if API_level<11
            Log.i("TAG", "onCreateInputView: Throwable " + t.message)
        }

    }

Upvotes: 0

Carl
Carl

Reputation: 11

You get the token from the view by view.getWindowToken().

Upvotes: 1

EdJ
EdJ

Reputation: 1306

Turns out that the switchInputMethod(String id) method works a treat - no need for that token.

Upvotes: 1

Related Questions