Tmi 4 keyboard?fm=jpg&fl=progressive&q=75&w=300

TMI #4: Dismissing the Virtual Keyboard in Android and iOS

In TMI, Michael Helmbrecht gives overly detailed answers to popular mobile development questions.

Ever browsed the web for help, only to find the popular answers leave you hungry for more? In our fourth episode, Michael explains how to hide the pesky keyboard in both Android and iOS with 2 lines of code so users can see more of your app’s great content. Frank the Java master will help too. 🐶


Hi everyone! It’s Michael from Realm. Here on TMI I will answer more of your mobile development questions. Today I’ll be addressing a really common question that comes up a lot while building Android and iOS apps: How can you dismiss the virtual keyboard?

Get more development news like this

The virtual keyboard sometimes covers part of the screen. By hiding it, you can allow your user to see more of the content within your app. We’ll look at how this works in Java, Objective-C, and Swift.

Android

Dismissing the keyboard is actually pretty easy on Android. If we look at the code here, we can see that it’s just two simple lines.

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

We use getSystemService to get a reference to the InputMethodManager. That’s the object that’s keeping track of the soft keyboard. If you have any hardware peripherals, it’ll be helping manage those as well. Then, we just call a simple method on it: hideSoftInputFromWindow.

Importantly, you’ll need a token, which I’ll discuss further below. You pass in the token and then you pass in this 0 as an options flag.

If we look at this with a little bit of context around it, we would probably use this by getting the currently focused view:

View view = this.getCurrentFocus();
if (view != null) {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

We would just have our getCurrentFocus method to get a reference to the view we’re trying to look at, and then check to make sure it’s null. That is how we get that window token that we’re going to need to pass to the InputMethodManager.

Another common variation I’ve seen on this is to replace that 0 options flag with InputMethodManager.HIDE_IMPLICIT_ONLY:

View view = this.getCurrentFocus();
if (view != null) {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}

That will hide the keyboard if it’s been implicitly brought up. If the user clicks on an EditText, then that’s an explicit action indicating that the user wants the keyboard up, so this method will not do anything in that case, since it’s been an explicit request. However, if there’s some other kind of process in the background, or if there’s some other mechanism by which the object has implicitly gotten focus (i.e. not through a specific user request), then this will fire. Using this flag can be a way to make sure that the keyboard is there if the user specifically wants it, but some of the other software processes aren’t going to pop up the keyboard and maybe annoy the user.

iOS

For iOS, it’s actually even easier. It’s just one line of code.

Objective-C

[self.view endEditing:YES];

Swift

self.view.endEditing(true);

You can tell any view endEditing and pass it YES in Objective-C, or true in Swift. The YES (or true) is a “force” boolean. It can force whatever the first responder is to let that go. This will automatically go through and figure out what responder has the keyboard up and tell it to resign.

I’ve seen a lot of code online where people rewrite this themselves, iterating through subviews and the whole subview hierarchy. You don’t need to do that. There’s already a thing that will do that for you, and it is called endEditing. Just call that on whatever view you want to let go of the keyboard.

I hope you enjoyed that little bit of code! Good luck with your keyboards, and see you next time on TMI.

Resources


This post is licensed under a Creative Commons BY-SA 3.0 license.

Next Up: Realm Java 2.0 - Mobile Platform Support

General link arrow white

About the content

This content has been published here with the express permission of the author.

Michael Helmbrecht

Michael designs and builds things: apps, websites, jigsaw puzzles. He’s strongest where disciplines meet, and is excited to bring odd ideas to the table. But mostly he’s happy to exchange knowledge and ideas with people. Find him at your local meetup or ice cream shop, and trade puns.

4 design patterns for a RESTless mobile integration »

close