Android: Hide a view when the soft keyboard is showing

This took far too long to find, mostly because the documentation doesn’t make sense (to me) and because the one hit on the android-developers list looked like the wrong answer, when it fact it’s right.

On the NPR News for Android app, we have a player at the bottom of the screen. This is handled with a FrameLayout in activity layouts. For activities that had an EditText field, the keyboard would push the player up and cover the important parts of the screen. See the image at right for the ‘before’ issue.

I can see this being a major problem for apps that host a banner ad at the bottom of pages (and saw a post on StackOverflow to that effect). Clearly you don’t want the ad to cover the content when your user is trying to type.

The solution was simple enough for us: mark the activity with windowSoftInputMethod = adjustPan in the Manifest and this resolved it.

    <activity
      android:name="SearchResultsActivity"
      android:windowSoftInputMode="adjustPan"/>

Unfortunately, windowSoftInputMethod is not very well documented. The reference says that there are two possible choices, adjustPan and adjustResize and that activities should always select one or the other.

adjustResize
The activity’s main window is always resized to make room for the soft keyboard on screen.
adjustPan
The activity’s main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

What should be made more clear is that “resize” doesn’t mean scale or zoom here, it means that the container for your layout is compressed and every element is realigned to try to make it fit best. While this may make sense for applications that have content above or below the edit field that they want visible while editing, it isn’t what we were looking for.

By using adjustPan the layout manager leaves the activity as it was and only shows a portion of it — the part that would be visible if the keyboard were laid over the activity. Exactly what we want.

Advertisements

12 thoughts on “Android: Hide a view when the soft keyboard is showing

  1. Hello !

    I like your articles. I have also read your snapshot article. I have a problem and I am desperate because it`s from my diploma project and time is running out. I would be very grateful if you would post an article about establishing Android bluetooth connections. I have tried the developers.android.com code but i have a problem when testing on real devices. The error says “Unable to start service discovery”. Thank you and i hope you can help me !

  2. Mihai –

    I haven’t done Bluetooth work on Android, to-date, so can’t really help you. Seems like an error you should be able to search on and find similar experiences though. Good luck!

  3. Cool…

    I have a question that if i do the “adjustPan” and the edit text is behind the keyboard.. so in this case the the keyboard next [button] will focus to that edit text also the layout comes up on keyboard[ofcource without resizing] and when you click on back button the the activity takes time to fit in the screen in this time the UI will freeze and we no response we will get from the screen[some times ANR].How can we handle this situation….

    Hoping a quick reply…

    Thanks in advance.

    –Dj

    • DJ –

      I’m not sure I follow your problem, but let me give it a shot.

      1) Using “adjustPan” the edit field that has focus should be visible, not behind the keyboard. If it is behind the keyboard that implies to me that for some reason the framework isn’t properly able to tell which field has focus. Maybe look into explicitly setting focus on the field.

      2) With “adjustResize” it has to redraw your layout to fit in the remaining space. With “adjustPan” it shouldn’t have to redraw the layout — it should just scroll (“pan”) the layout to put the focused field in the remaining space. If you are getting an ANR when the keyboard is hidden, my guess is that there’s something in you drawing/layout code that’s taking too long to run. Are you loading or parsing data to layout the view?

      • Thank you for the quick reply.

        — I agree with the first point, but i am observing the when the edit text which is behind keyboard.focused the complete layout is coming top of the keyboard.And when i press back the window is still be there and the keyboard room will be as is for some time….where again freezes the UI.

        — No i am not loading any data no quries and no parsing.Just having 3 edit texts and 2 buttons with date picker functionality and then again one edit text [Which is coming behind the keyboard]. The same problem occurs when the date picker comes up on screen the keyboard is automatically goes into hide…and again freeze problem.Am i missing something…and this is in portrait mode.tablet.

        Am i doing wrong ?

        I had written sample program also but the same issue..Could you pls tell me where is the problem. If you want i can provide some more detailed level of info if you want.

        Thanks..

        –Dj

  4. Dj – not really sure where the problem is. I’ve never seen an ANR in this case so I don’t know that I can help you further. If you can repro this for a simple case, I’d suggest posting on android-developers. I know the list has lots of noise and traffic but the experts hang out there and should be able to pinpoint this better.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s