0

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.

Community
  • 1
  • 1
Kacy
  • 3,330
  • 4
  • 29
  • 57
  • 2
    Is there a difference if you use `event.getX()` and `event.getY()` instead? – Karakuri Dec 15 '16 at 19:25
  • @Karakuri That was it! Feel free to post an answer, and I'll accept it. Thank you. I'm assuming the reason this works is because the `getRawY()` method doesn't account for the any displacement of the activity caused by the keyboard, and `getY()` does – Kacy Dec 15 '16 at 19:43
  • Answer posted, glad it worked out for you – Karakuri Dec 15 '16 at 22:48

2 Answers2

1

Use event.getX() and event.getY() instead of getRawX() and getRawY()

Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

If you're just attempting to close the keyboard when tapping outside an EditText, then there's simplier solution. Try to apply this xml oprtions to the parent layout of your EditText

<LinearLayout
     ....
     android:focusable="true"
     android:clickable="true"/>

Then it will receive the focus and EditText will lose it. OnFocusChangeListener will work as it should.

Roman Iatcyna
  • 394
  • 3
  • 10
  • I can see that working for some apps, but the only problem I have with that solution is possibly having to paste it in multiple places, especially since I'm using fragments and the click event might get consumed by some other UI box. Right now all my activities inherit from a custom BaseActivity which is the only place that contains this code. – Kacy Dec 15 '16 at 20:09