Reputation: 5273
I'm not asking how to prevent keyboard from pushing up the screen when it pops up.
When I click an EditText
the keyboard shows up, and the screen remains unchanged. But the problem is when I click outside the EditText
, the keyboard hides and then (after about half a second) the screen jumps a little (leaving a white background at the bottom) and then settles back down.
I'm using a custom Keyboard and a custom EditText.
In the activity I've set:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
And when the EditText
gains focus this is how I show up the keyboard:
InputMethodManager inputMethodManager = (InputMethodManager)MainApplication.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput( view, 0 );
And hide it when EditText
loses focus:
InputMethodManager inputMethodManager = (InputMethodManager)MainApplication.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
EDIT:
Note that when I press DONE on the keyboard, the keyboard just hides (without the screen jumping) but the EditText doesn't lose focus. So if it's possible to just simulate a DONE press when the EditText
is about to lose focus, this would be fine with me.
Upvotes: 3
Views: 1141
Reputation: 5273
This is how I solved it:
I set an OnTouchListener
on the main layout (it's a LinearLayout
) and hided the keyboard there:
mainLinearLayout.setOnTouchListener( new OnTouchListener() {
@Override
public boolean onTouch( View v, MotionEvent event ) {
InputMethodManager inputMethodManager = (InputMethodManager)MainApplication.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
return false; //*SEE NOTE BELOW
}
} );
*NOTE:
It turns out that if I return true
(the event is consumed), the keyboard hides, but the EditText
doesn't lose focus. And if I return false
(the event is not yet consumed), the keyboard hides, and the EditText
loses focus (either way, no screen jumping)
Upvotes: 1