Skip to content

Instantly share code, notes, and snippets.

@sc0rch
Created July 9, 2016 01:45
Show Gist options
  • Save sc0rch/7c982999e5821e6338c25390f50d2993 to your computer and use it in GitHub Desktop.
Save sc0rch/7c982999e5821e6338c25390f50d2993 to your computer and use it in GitHub Desktop.
Clear focus on touch outside for all EditText inputs.
public class MainActivity extends Activity
// ... any code
/**
* Clear focus on touch outside for all EditText inputs.
*/
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent( event );
}
}
@RayNjire
Copy link

Oh wow, It works perfectly! Thank you

@DeCryptinWeb
Copy link

Works perfectly! Many Thanks ❤

@drmrbrewer
Copy link

drmrbrewer commented Sep 10, 2021

Not so good when you click into another EditText... keyboard is dismissed and then immediately re-opens. @DeCryptinWeb @RayNjire @sc0rch

EDIT... here is a version which includes some extra code to check whether you're clicking into another EditText... in which case you don't hide the keyboard: https://stackoverflow.com/a/61290481/4070848

@Feelynx
Copy link

Feelynx commented Mar 23, 2022

To avoid keyboard dismissed and re-open everytime just evaluate the ACTION_UP and not ACTION_DOWN as follows:

if (event.getAction() == MotionEvent.ACTION_UP)

@Govind-Indpro
Copy link

Great! It works perfect, as what I needed..

@marticztn
Copy link

Thank you soooo much!

@sieleemmanuel
Copy link

Very precise! works like a charm.

@duckmaniac
Copy link

Thank You! It finally works :)

@mustfaunlu
Copy link

it's works very well thanks 👍

@onelessangel
Copy link

worked wonderfully! helped me out so much, ty!

@sc0rch
Copy link
Author

sc0rch commented Jul 31, 2024

surprised how often people find this piece of code, I don't even code on Android anymore for the last 8 years.

@Zelyson
Copy link

Zelyson commented Sep 15, 2024

Here's the Kotlin implenetation if anyone's interested. Just add it below (or above) the 'onCreate' function and you're set:

    /**
     * Clear focus on touch outside for all EditText inputs.
     */
    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_DOWN) {
            val v = currentFocus
            if (v is EditText) {
                val outRect = Rect()
                v.getGlobalVisibleRect(outRect)
                if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
                    v.clearFocus()
                    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(v.windowToken, 0)
                }
            }
        }
        return super.dispatchTouchEvent(event)
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment