Ok everyone knows that to hide a keyboard you need to implement:
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
But the big deal here is how to hide the keyboard when the user touches or selects any other place that is not an EditText
or the softKeyboard?
I tried to use the onTouchEvent()
on my parent Activity
but that only works if user touches outside any other view and there is no scrollview.
I tried to implement a touch, click, focus listener without any success.
I even tried to implement my own scrollview to intercept touch events but I can only get the coordinates of the event and not the view clicked.
Is there a standard way to do this?? in iPhone it was really easy.
You can achieve this by doing the following steps:
Make the parent view(content view of your activity) clickable and focusable by adding the following attributes
Implement a hideKeyboard() method
Lastly, set the onFocusChangeListener of your edittext.
As pointed out in one of the comments below, this might not work if the parent view is a ScrollView. For such case, the clickable and focusableInTouchMode may be added on the view directly under the ScrollView.
Override public boolean dispatchTouchEvent(MotionEvent event) in any Activity (or extend class of Activity)
And that's all you need to do
I modified the solution of Andre Luis IM I achieved this one:
I created a utility method to hide the soft keyboard the same way Andre Luiz IM did:
But instead of register an OnTouchListener for every view, that give a poor performance, I registered the OnTouchListener for just the root view. Since the event bubbles until it's consumed (EditText is one of the views that consumes it by default), if it arrives to the root view, it's because it wasn't consumed, so I close the soft keyboard.
Well I manage to somewhat solve the problem, I overrode the dispatchTouchEvent on my activity, there I am using the following to hide the keyboard.
EDIT: The getFields() method is just a method that returns an array with the textfields in the view. To avoid creating this array on every touch, I created an static array called sFields, which is returned at the getFields() method. This array is initialized on the onStart() methods such as:
sFields = new EditText[] {mUserField, mPasswordField};
It is not perfect, The drag event time is only based on heuristics so sometimes it doesnt hide when performing long clics, and I also finished by creating a method to get all the editTexts per view; else the keyboard would hide and show when clicking other EditText.
Still, cleaner and shorter solutions are welcome
You may easily override the onKey() event in activity and fragments to hide the keyboard.
I'm aware that this thread is quite old, the correct answer seems valid and there are a lot of working solutions out there, but I think the approach stated bellow might have an additional benefit regarding efficiency and elegance.
I need this behavior for all of my activities, so I created a class CustomActivity inheriting from the class Activity and "hooked" the dispatchTouchEvent function. There are mainly two conditions to take care of:
This is my result:
Side note: Additionally I assign these attributes to the root view making it possible to clear focus on every input field and preventing input fields gaining focus on activity startup (making the content view the "focus catcher"):