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.
Just Add this code in the class @Overide
The following snippet simply hides the keyboard:
You can put this up in a utility class, or if you are defining it within an activity, avoid the activity parameter, or call
hideSoftKeyboard(this)
.The trickiest part is when to call it. You can write a method that iterates through every
View
in your activity, and check if it is aninstanceof EditText
if it is not register asetOnTouchListener
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, in fact you can use this to do anything, like setup custom typefaces etc... Here is the methodThat is all, just call this method after you
setContentView
in your activity. In case you are wondering what parameter you would pass, it is theid
of the parent container. Assign anid
to your parent container like<RelativeLayoutPanel android:id="@+id/parent"> ... </RelativeLayout>
and call
setupUI(findViewById(R.id.parent))
, that is all.If you want to use this effectively, you may create an extended
Activity
and put this method in, and make all other activities in your application extend this activity and call itssetupUI()
in theonCreate()
method.Hope it helps.
If you use more than 1 activity define common id to parent layout like
<RelativeLayout android:id="@+id/main_parent"> ... </RelativeLayout>
Then extend a class from
Activity
and definesetupUI(findViewById(R.id.main_parent))
Within itsOnResume()
and extend this class instead of ``Activityin your program
I liked the approach of calling
dispatchTouchEvent
made by htafoya, but:So, I made this somewhat easier solution:
There is one disadvantage:
Switching from one
EditText
to anotherEditText
makes the keyboard hide and reshow - in my case it's desired that way, because it shows that you switched between two input components.Try to put stateHidden on as your activity
windowSoftInputMode
valuehttp://developer.android.com/reference/android/R.attr.html#windowSoftInputMode
For example for your Activity:
its too simple, just make your recent layout clickable an focusable by this code:
and then write a method and an OnClickListner for that layout , so that when the uppermost layout is touched any where it will call a method in which you will write code to dismiss keyboard. following is the code for both; // you have to write this in OnCreate()
method called from listner:-