I am attempting to close the keyboard when tapping outside an EditText. I copied and pasted the code from this post (zMan's answer) into my MainActivity. It works perfectly when tapping outside the EditText, but when I tap inside it, the following code outputs the following:
Rect dimensions: 12 1326 958 1419 //left, top, right, bottom
event_x: 636.0 event_y: 978.0 //event_y is outside the range
The problem is that event_y is outside the range of 1326 and 1419, which results in the keyboard closing. However, I know I am tapping inside the EditText because it immediately reopens after closing, most likely because of the call to super.onDispatchTouchEvent(). The event's y coordinate is always about 350-400 units off when I tap inside the EditText.
I'm about 98% sure the problem has to do with the keyboard displacing the EditText since the EditText is near the bottom of the screen. The problem does NOT occur when the EditText is at the top of the screen.
@Override
public boolean dispatchTouchEvent( MotionEvent event )
{
if ( event.getAction() == MotionEvent.ACTION_DOWN )
{
View view = getCurrentFocus();
if ( view instanceof EditText )
{
Rect outRect = new Rect();
view.getGlobalVisibleRect( outRect );
//if touch event occurred outside of the EditText
if ( ! outRect.contains((int)event.getRawX(), (int)event.getRawY()) )
{
//I SHOULD NOT REACH THIS CODE WHEN TAPPING INSIDE EditText
Debug.print( "Rect dimensions: " + outRect.flattenToString() );
Debug.print( "event_x: " + event.getRawX() + " event_y: " + event.getRawY() );
ViewUtil.closeKeyboard( this );
}
}
}
return super.dispatchTouchEvent( event );
}
Please let me know if I need to provide more info.