Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide soft keyboard on losing focus

Tags:

android

When we have an EditText and it loses focus (to an element that doesn't need a keyboard), should the soft keyboard hide automatically or are we supposed to hide it ourselves?

I'm moving the focus from an AutoCompleteSearchView (which should behave like an EditText I guess) to a Button, requestFocus() returns true, but the keyboard doesn't hide.

like image 622
fweigl Avatar asked Mar 14 '13 15:03

fweigl


People also ask

How do I hide my soft keyboard?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I make my keyboard always visible?

You must have an EditText in your layout and that need to extent EditText base class. then Override onKeyPreIme() method, and return True. Now your keyboard will be always visible and can't be dismissed by Back key.

How do I hide my keyboard when typing?

Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden.

How do I hide the soft keyboard on Android after clicking outside 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 trigger the keyboard to dismiss itself?

To trigger the keyboard to dismiss itself, we need to remove “focus” from our text field. By removing the current focus from the text field's FocusNode, we can achieve the desired result. Wait, a focus-what?

How to dismiss the keyboard in flutter?

How to Dismiss the Keyboard in Flutter the Right Way. 1 Step 1: Detect the tap. The first thing we need to do is detect when a user has tapped outside of the currently focused text field. This is trivial ... 2 Step 2: Dismiss the Keyboard.

How to remove focus from the current node in flutter?

If it doesn't, we call unfocus () on the current node to remove focus and trigger the keyboard to dismiss. If you attempt to unfocus () a node that currently has the primary focus, Flutter will throw an exception.


1 Answers

Best way is to set a OnFocusChangeListener for the EditText, and then add the code to the the keyboard into the OnFocusChange method of the listener. Android will then automatically close the keyboard when the EditText loses focus.

Something like this in your OnCreate method:

EditText editText = (EditText) findViewById(R.id.textbox); OnFocusChangeListener ofcListener = new MyFocusChangeListener(); editText.setOnFocusChangeListener(ofcListener); 

and then add the class:

private class MyFocusChangeListener implements OnFocusChangeListener {      public void onFocusChange(View v, boolean hasFocus){          if(v.getId() == R.id.textbox && !hasFocus) {              InputMethodManager imm =  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);             imm.hideSoftInputFromWindow(v.getWindowToken(), 0);          }     } } 
like image 95
leedsunited92 Avatar answered Sep 17 '22 23:09

leedsunited92