I have an EditText
called myTextview
. I want the soft keyboard to show when I click on the EditText
but then dismiss if I click outside of the EditText
. So I use the method below. But the keyboard does not dismiss when I click outside the view (I click a TextView
). How do I fix this code?
myTextview.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); } else { InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myTextview.getWindowToken(), 0); } } });
and put the following code in the onTouch method. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);
setShowSoftInputOnFocus(false); to disable the software keyboard showing when the EditText is touched. the hideKeyboard(this); call in OnCreate to forcible hide the software keyboard.
Here pass HIDE_IMPLICIT_ONLY at the position of showFlag and 0 at the position of hiddenFlag . It will forcefully close soft Keyboard.
I found a better solution:
Override the dispatchTouchEvent
method in your Activity.
@Override public boolean dispatchTouchEvent(MotionEvent ev) { View v = getCurrentFocus(); if (v != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && v instanceof EditText && !v.getClass().getName().startsWith("android.webkit.")) { int[] sourceCoordinates = new int[2]; v.getLocationOnScreen(sourceCoordinates); float x = ev.getRawX() + v.getLeft() - sourceCoordinates[0]; float y = ev.getRawY() + v.getTop() - sourceCoordinates[1]; if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom()) { hideKeyboard(this); } } return super.dispatchTouchEvent(ev); } private void hideKeyboard(Activity activity) { if (activity != null && activity.getWindow() != null) { activity.getWindow().getDecorView(); InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0); } } }
This is applicable even if you are working with webview.
UPDATE 10th July 2020
The above method works perfectly but the EditText is still has the focus and typing cursor is still visible.
To solve the described issue. Do this
Add findViewById(android.R.id.content).setFocusableInTouchMode(true);
in your onCreate() method
Add findViewById(android.R.id.content).clearFocus();
in the hideKeyboard()
method
Credit to Eddwhis's comment
Maybe a little bit easier:
Set a focusChangedListener on your edit text and then just hide the keyboard if you don't have focus.
yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(!hasFocus){ hideKeyboard(); } } }); private void hideKeyboard() { InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With