Photo by 🇨🇭 Claudio Schwarz | @purzlbaum on Unsplash

Android. How to synchronize keyboard with EditText focus?

Alex Misiulia
AndroidPub
Published in
2 min readJun 11, 2019

--

In my current project manager asks me why a keyboard is not visible when he navigates back to a screen which has focus. I didn’t have a good answer to this question. Let’s dive deeper.

When you navigate from ActivityA with EditText to ActivityB you can see that the keyboard is automatically hidden. Nice behavior but what if we click back?

The keyboard is not showing

Why is EditText focused, but the keyboard is hidden? Strange UX for the user (please specify in comments your opinion, maybe I and manager are wrong about this).

There are several solutions to this issue:

  • Create EditText wrapper and set focus change listener, but it is tricky because a focus is not changed in all situations. Here is the link to stack question.
  • Toggle keyboard visibility manually when back (or other buttons) clicked. Will work but we need to write it every time we have a new screen.

We need to upgrade the second method and encapsulate the logic of showing keyboard visibility at one place. We will do it inside the method onStart:

We will listen to callback onStart and show the keyboard if Activity has current focus. The method showSoftKeyboard is tricky too for now (link to a similar problem). I haven’t found any good solution for this logic but will update the article when I will do it. Work with the keyboard is still painful, feel free to share your solution in comments because it will save a lot of time or point to me to the right article).

Here is the behavior of the app after this fix:

Talk is cheap. Show me the code ©

A link to the repository. A branch keyboard_issue shows the initial state, keyboard_fixed- fixed state.

This issue isn’t completely solved, I have a few other topics to investigate :

  • Fragment navigation and keyboard appearance (unfortunately the behavior will be different in this case)
  • Properly keyboard show/hide behavior.

Please point in comments which topic is more important for you. It will help me to write blog posts for the most relevant topics.

My name is Alex Misiulia and I am passioned about tech (especially Android). Feel free to reach me on Twitter and LinkedIn and ask questions about Android development.

--

--