Understanding Android Keyboard Showing Mechanism

Egemen Hamutçu
4 min readJun 3, 2021

After 8 years of professional development in Android, I think I have finally found a stable solution to the one of the oldest problem: Showing keyboard without using InputMethodManager.SHOW_FORCED brutality.

I called it brutal because it negatively affects user experience and increase development costs. If user taps on home button when a keyboard is shown forcibly, app goes background while the keyboard remains on the screen looking at the user like ‘You shall not pass!’. As a result, you need to close it before the activity goes onStop() state by coding more and more.

You may not want to see the mechanism and just want to get the solution now (You may have 30 seconds to deadline). Just fast scroll down to the bottom of the article and copy/paste the code from the code snippet. Cheers!

Okay as I send code commandos to their salvation, I am now with elite developers who wonder what is going on behind the scenes. Let’s discuss how I found the approach and how I slightly edited it to make it work much better.

I suppose all of you had an experience of dealing with showing keyboards during your professional lifetime, and all of you know that showSoftInput method of InputMethodManager does not work most of times.

When you search this issue on stackoverflow, you always get a solution of passing InputMethodManager.SHOW_FORCED to flags parameter of showSoftInput method and it makes your day. I am sorry, but you make only one day. Since it is not a total solution as you do not understand how Android keyboard works, so your keyboard will not show as you expected in some cases.

First of all, I must thank to Square team and Helios Alonso by writing insights about Android Keyboard and enlighten me. If I did not find the blog, I would barely got over this issue. You can find the link to the blog for details.

Android Keyboard has 4 requirements to be shown:

  1. Input view must have focus.
  2. Input view hierarchy’s window must have focus.
  3. mServedView field of InputMethodManager must not be null and must have equal reference to the input view.
  4. There must be an input connection between input view and soft keyboard.

If one of them is not provided, Android keyboard will not be shown even if you pass InputMethodManager.SHOW_FORCED parameter. This is where I search for mServedView on Google so the search navigated me to the Square’s blog.

As you know, showSoftInput returns a boolean value as if it indicates whether the keyboard is shown, but in reality, it does not. It only indicates whether mServedView is set and there is input connection (3rd and 4th requirements). However, if the input view hierarchy’s window has not focus yet, Android keyboard will not appear. Similarly, your input view and input view hierarchy’s window may have focus, but if you call showSoftInput method before InputMethodManager has not got properly setup yet, Android keyboard will not be shown again.

To achieve all 4 requirements, Square provided an extension function below:

In my case, I have an activity containing an input view. It starts with configChanges attribute with keyboardHidden value. However, in some cases, I must focus on the input view and show keyboard by controlling a boolean value from bundle in intent.

I copied and pasted the code above and used it in my cases. My first trials were very successful and I got excited that I fixed the problem forever. Unfortunately, as I continue to use the application, the keyboard started not to show up after I had tried two different cases 6–7 times.

However, I felt that I was very close to the final solution and started to put some logs in the function above and monitor what was going on. I realized that showSoftInput method does not always return true. Even Square guys used post method to be sure thatInputMethodManager gets properly setup, it didn’t get ready until post method ran its action.

Finally, I started to think that I should give it another try to show the keyboard because if it missed in the first time because of timing, it must catch it in the second time, so I added a boolean parameter called tryAgain in the function. Its default value is true. Then, I controlled the return value of showSoftInput method. If it is false, I call the extension function all over again by passing tryAgain parameter as false.

I ran my application and tested those cases many times at least 15 and Eureka! Android keyboard happily shows up ever after.

I am really excited to hear about your results and close this issue forever. You can find my edit below. Hope this helps and happy coding to everyone!

Reference: https://developer.squareup.com/blog/showing-the-android-keyboard-reliably/

--

--

Egemen Hamutçu

Android Developer at Paradox Cat, chaotic neutral, little prince of his little kingdom