showSoftInputFromInputMethod is deprecated Android P API 28

8.3k Views Asked by At

The showSoftInputFromInputMethod method from the InputMethodManager class is deprecated in Android P. As per the documentation, we should be using the InputMethodService.requestShowSelf(int) method for Android P and above.

Now the question is how can we get the reference for the class InputMethodService. I tried creating a new object and calling requestShowself() on it but it doesn't work.

InputMethodService inputMethodService = new InputMethodService();
inputMethodService.requestShowSelf(0);

How can we use the suggested alternative for this deprecation for API 28 and above?

2

There are 2 best solutions below

0
Taranfx On

InputMethodService is implemented by IME apps. (e.g. gboard). If you're app developer and trying to show IME, use InputMethodManager.showSoftInput(TextView, 0);

1
Akshay On

Kotlin version:

    fun showKeyboard(mEtSearch: EditText, context: Context) {
            mEtSearch.requestFocus()
            val imm: InputMethodManager =
                context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(mEtSearch, 0)
    }