change focus of edittext android when click outside

Solutions on MaxInterview for change focus of edittext android when click outside by the best coders in the world

showing results for - "change focus of edittext android when click outside"
Henry
02 Aug 2019
1@Override
2public boolean dispatchTouchEvent(MotionEvent event) {
3    if (event.getAction() == MotionEvent.ACTION_DOWN) {
4        View v = getCurrentFocus();
5        if ( v instanceof EditText) {
6            Rect outRect = new Rect();
7            v.getGlobalVisibleRect(outRect);
8            if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
9                v.clearFocus();
10                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
11                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
12            }
13        }
14    }
15    return super.dispatchTouchEvent( event );
16}
17