Android: How to Hide Keyboard By Touching Screen Outside Keyboard

If you’ve ever worked for a mobile software company that builds both an iOS app and an Android app, many times the product people will ask that a certain feature be implemented consistently across both platforms. ┬áThis week I was tasked with implementing a popular iOS feature, the ability to hide the virtual keyboard when the user touches outside of the keyboard, on Android.

The methodology is simple, but not entirely intuitive. Let’s look at how it’s done. Basically, you get a handle on the layout in question. In this example, we’ll use a LinearLayout:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

 

Then, override the layout’s OnTouchListener.onTouch() like so:

layout.setOnTouchListener(new OnTouchListener()
{
    @Override
    public boolean onTouch(View view, MotionEvent ev)
    {
        hideKeyboard(view);
        return false;
    }
});

 

The code to hide the keyboard is fairly popular:

/**
* Hides virtual keyboard
*
* @author kvarela
*/
protected void hideKeyboard(View view)
{
    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

Software Engineers, Don’t Waste Your Time Any Longer, Build Your Future

Today, I came to the full realization of something that’s always been an itch in the back of my mind. If you’re a software engineer and you’re not utilizing your talents to help build a company or gain a stake in a company, you are wasting your time.

You may say, “Oh Karim, that’s mighty hypocritical for you to say. After all, you work for Fandango as a consultant and have no stake in NBC Universal, the parent company of Fandango. You get no benefit and no share of Fandango’s immense success.” If you said this, you are right, and don’t get me wrong, Fandango is an awesome place to work. From our catered lunches and company movie outings and an incredible location just three miles from my apartment, it’s difficult to imagine a better place to work right now.

I’m not resting on my laurels, however; I’m actively doing things to build my future. For instance, I’m about half way through with business school at the University of Florida. I’m also attending the AT&T Mobile App Hackathon – Los Angeles in a few weeks. There, I plan to build my first commercial mobile app.

As software engineers, we have the unique ability to not only think something up, but to go out and build it. We have the power to create software that millions of people will use. We have the power to create this with nothing more than our brains, our fingers, and a computer. Even with a world economy in the midst of a deep recession, the demand for software engineers has been increasing faster than the supply. Don’t waste your talent; go build your future. If you have a good idea, and are interested in working with me, hit me up. Let’s chat!

Awareness Is …

A person is aware when they contemplate the world around them, when they
understand they are just a grain of sand in the universal ocean, when they
recognize how good we have it and empathize with those who are less
fortunate, when they know what’s going on in the world outside of their
city, state, country, race, creed, or origin, when they form their beliefs
based on their own moral compass and are not influenced by media or popular
culture, when they can put themselves in somebody else’s shoes who may
think very different from them and understand why they think that way.