2

I am using android studio. I have Edit text in my fragment page and now i want to hide keyboard after clicking outside EditText. I used the below code but it is not working.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
 final InputMethodManager imm = (InputMethodManager)
 getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(locationEt.getWindowToken(), 0);
}

Thanks in Advance

Mohit Trivedi
  • 699
  • 3
  • 13
raji
  • 101
  • 1
  • 3
  • 13
  • please try this one http://stackoverflow.com/questions/4165414/how-to-hide-soft-keyboard-on-android-after-clicking-outside-edittext – Ganesh Gudghe Sep 28 '16 at 12:12

7 Answers7

3

Try to set an onFocusChangeListener to your EditText. In the onFocusChange method you can hide the keyboard like this:

mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(locationEt.getWindowToken(), 0);
        }
    });
jennymo
  • 1,450
  • 1
  • 18
  • 43
1

Try this pass the activity in below function. It works.

 public static void hideKeyboard(Activity activity) {
            // Check if no view has focus:
            View view = activity.getCurrentFocus();
            if (view != null) {
                InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
Preetika Kaur
  • 1,991
  • 2
  • 16
  • 23
1

Write this code to your Activity where your fragment is placed.

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    View view = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);

    if (view instanceof EditText) {
            try {
                View w = getCurrentFocus();
                int scrcords[] = new int[2];
                w.getLocationOnScreen(scrcords);
                float x = event.getRawX() + w.getLeft() - scrcords[0];
                float y = event.getRawY() + w.getTop() - scrcords[1];

                if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (getWindow() != null && getWindow().getCurrentFocus() != null) {
                        imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    return ret;
}

Happy coding!

Paresh P.
  • 6,677
  • 1
  • 14
  • 26
1

use this method when outside or other view click event

public void hideKeyboard(View view) {
    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

Like this way

txtHeader.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       hideKeyboard(txtHeader);
   }});
Arti
  • 672
  • 7
  • 19
1

Kotlin extension for hiding keyboard

fun View.hideKeyboard() {
    val inputMethodManager = 
        context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
prsnlme
  • 179
  • 8
0

Please use below code on click your parent layout :

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
Chirag Arora
  • 816
  • 8
  • 20
0

Try this one its working for activity as well fragment

public void setupUI(View view) {
    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //code of hide soft keyboard
                return false;
            }
        });
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupUI(innerView);
        }
    }
}
Anand Savjani
  • 2,484
  • 3
  • 26
  • 39