Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss keyboard when click outside of EditText in android

Tags:

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);             }          }     }); 
like image 520
user3093402 Avatar asked Dec 20 '13 22:12

user3093402


People also ask

How do I hide the keyboard by one tap outside of an EditText?

and put the following code in the onTouch method. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I hide soft keyboard when EditText is focused?

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.

How do I get rid of soft keyboard on Android?

Here pass HIDE_IMPLICIT_ONLY at the position of showFlag and 0 at the position of hiddenFlag . It will forcefully close soft Keyboard.


2 Answers

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

  1. Add findViewById(android.R.id.content).setFocusableInTouchMode(true); in your onCreate() method

  2. Add findViewById(android.R.id.content).clearFocus(); in the hideKeyboard() method

Credit to Eddwhis's comment

like image 149
Felipe R. Saruhashi Avatar answered Sep 19 '22 12:09

Felipe R. Saruhashi


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); } 
like image 30
pat Avatar answered Sep 19 '22 12:09

pat