Hide keypad in android while touching outside Edit Text Area

I know that the code for dismiss tyhe keypad in android is

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

Can anyone suggest me a method to hide the keypad if we touch the area outside the text area other than the keypad in the screen.

  • How to test an IntentService with Robolectric?
  • Android app architecture - where to put REST API call code?
  • Android: Callback AsyncTask to Fragment(Not Activity)
  • Strange behavior with android orientation sensor
  • how to run media scanner in android
  • Detect the status of two SIM cards in a dual-SIM Android phone
  • Related posts:

    How to get the ActionBar height?
    Android TableLayout Width
    Android 4.4.2 SSL handshake aborted
    Programmatically select item ListView in Android
    Application will not run on device: DELETE_FAILED_INTERNAL_ERROR?
    get coordinates by clicking on map (openstreetmaps)
  • the differences between createRfcommSocketToServiceRecord and createRfcommSocket
  • Android Recyclerview GridLayoutManager column spacing
  • SDK Manager won't start Windows 7 64-bit?
  • EditText without auto-correction, etc
  • input textbox hidden behind keyboard on android Chrome
  • Lollipop RippleDrawable vs Selector for Pre-Lollipop
  • 3 Solutions collect form web for “Hide keypad in android while touching outside Edit Text Area”

    Code to dismiss Softkeyboard is below:

    public static void hideSoftKeyboard(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);

    You can put it in Utility Class or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this).

    You can write a method that iterates through every View in your activity, and check if it is an instanceof EditText if it is not register a setOnTouchListener to that component and everything will fall in place. In case you are wondering how to do that, it is in fact quite simple. Here is what you do, you write a recursive method like the following.

    public void setupUI(View view) {
        //Set up touch listener for non-text box views to hide keyboard.
        if(!(view instanceof EditText)) {
            view.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    return false;
        //If a layout container, iterate over children and seed recursion.
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                View innerView = ((ViewGroup) view).getChildAt(i);

    Call this method after SetcontentView() with paramet as id of your view like:

    RelativeLayoutPanel android:id="@+id/parent"> ... </RelativeLayout>

    Then call setupUI(findViewById(R.id.parent))

    Best way you can use is DONE button besides EditText make your onClickListener to do like,

    done.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

    This may be old but I got this working by implenting a custom class

    public class DismissKeyboardListener implements OnClickListener { 
        Activity mAct;
        public DismissKeyboardListener(Activity act) {
            this.mAct = act;
        public void onClick(View v) {
            if ( v instanceof ViewGroup ) {
                hideSoftKeyboard( this.mAct );
    public void hideSoftKeyboard(Activity activity) {
            InputMethodManager imm = (InputMethodManager)
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

    the best practice here is to create a Helper class and every container Relative / Linear Layouts should implement this.

    **** Take note only the main Container should implement this class (For optimization) ****

    and implement it like this :

    Parent.setOnClickListener( new DismissKeyboardListener(this) ); 

    the keyword this is for Activity. so if you are on fragment you use it like getActivity();

    —thumbs up if it help you… — cheers Ralph —

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.