Chrishan Chrishan - 6 months ago 48
Android Question

hide keyboard when user taps anywhere else on the screen in android

I need to hide the softkeypad in android when user click on anywhere other than a Edittext. There are many help for iphone but not for android. I tried this code but its not working :(

final RelativeLayout base = (RelativeLayout) findViewById(;

findViewById( OnClickListener() {

public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(base.getWindowToken(), 0);


Thanks in advance !


You can use the onTouchEvent() to hide the Softkeyboard.

    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        return true;

Though this solution works, but the best I would suggest is to use below answer as it gives best solution of closing the keyboard touching anywhere else then EditText.