How to hide keyboard after input in EditText in android?

I have an EditText and a button aligned to the bottom of the parent.

When I enter text into it and press the button to save the data, the virtual keyboard does not disappear.

Can someone tell me how to hide the keyboard?

+63
android android-edittext
Feb 26 2018-10-28T00
source share
14 answers

That should work.

 InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 

Just make sure this.getCurrentFocus () does not return null, which would be if nothing was in focus.

+112
Feb 26 2018-10-26
source share

You can also define imeOptions in an EditText. Thus, the keyboard will disappear after clicking the "Finish" button:

 <EditText android:id="@+id/editText1" android:inputType="text" android:imeOptions="actionDone"/> 
+115
Apr 17 2018-12-12T00:
source share
  mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { // do something, eg set your TextView here via .setText() InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); return true; } return false; } }); 

and in xml

  android:imeOptions="actionDone" 
+19
Oct 26 '15 at 7:03
source share
 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
+10
Nov 09 '11 at 13:10
source share

The solution is included in the EditText action listener:

 public void onCreate(Bundle savedInstanceState) { ... ... edittext = (EditText) findViewById(R.id.EditText01); edittext.setOnEditorActionListener(new OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); } return false; } }); ... ... } 
+4
Jun 19 '13 at 19:00
source share

I have not seen anyone using this method:

 editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean focused) { InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); if (focused) keyboard.showSoftInput(editText, 0); else keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0); } }); 

And then just request focus on editText:

 editText.requestFocus(); 
+4
Jul 07 '16 at 6:02
source share

I found this because my EditText was not automatically fired upon input.

This was my original code.

 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) { // Do stuff when user presses enter return true; } return false; } }); 

I solved this by deleting the line

 return true; 

after entering information when the user presses the enter button.

Hope this helps someone.

+2
Nov 01 '16 at 16:39
source share

Fighting this in recent days and have found a solution that works very well. The soft keyboard is hidden when a touch is made outside of any EditText area.

Code posted here: hide the default keyboard when clicking on android

+1
Aug 30 2018-11-11T00:
source share

Just write down these two lines of code where the input option will work.

 InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 
+1
Mar 20 '18 at 23:45
source share

I use this method to remove the keyboard from the edit text:

  public static void hideKeyboard(Activity activity, IBinder binder) { if (activity != null) { InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); if (binder != null && inputManager != null) { inputManager.hideSoftInputFromWindow(binder, 0);//HIDE_NOT_ALWAYS inputManager.showSoftInputFromInputMethod(binder, 0); } } } 

And this method for removing the keyboard from activity (does not work in some cases - for example, when the edittext that the keyboard is attached to has lost focus, this will not work. But for other situations it works fine, and you do not need to take care of the element that holds keyboard)

  public static void hideKeyboard(Activity activity) { if (activity != null) { InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); if (activity.getCurrentFocus() != null && inputManager != null) { inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); inputManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0); } } } 
0
Jul 10 '15 at 10:16
source share

You can see the marked answer above. But I used getDialog().getCurrentFocus() and worked well. I am posting this answer because I cannot type "this" in my oncreatedialog.

So this is my answer. If you tried the marked answer and didn't work, you can simply try the following:

 InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
0
Jan 29 '16 at 23:09
source share
 int klavStat = 1; // for keyboard soft/hide button int inType; // to remeber your default keybort Type 

editor is an EditText field

 /// metod for onclick button /// public void keyboard(View view) { if (klavStat == 1) { klavStat = 0; inType = editor.getInputType(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); editor.setInputType(InputType.TYPE_NULL); editor.setTextIsSelectable(true); } else { klavStat = 1; InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0); editor.setInputType(inType); } } 

If I have an EditText field for the field, you need to watch the focus change

0
Aug 18 '16 at 19:07
source share

you can easily create a singleton class to call like this:

 public class KeyboardUtils { private static KeyboardUtils instance; private InputMethodManager inputMethodManager; private KeyboardUtils() { } public static KeyboardUtils getInstance() { if (instance == null) instance = new KeyboardUtils(); return instance; } private InputMethodManager getInputMethodManager() { if (inputMethodManager == null) inputMethodManager = (InputMethodManager) Application.getInstance().getSystemService(Activity.INPUT_METHOD_SERVICE); return inputMethodManager; } @SuppressWarnings("ConstantConditions") public void hide(final Activity activity) { new Handler().post(new Runnable() { @Override public void run() { try { getInputMethodManager().hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); } catch (NullPointerException e) { e.printStackTrace(); } } }); } } 

so after you can call in action, like the following form:

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); KeyboardUtils.getInstance().hide(this); } } 
0
Feb 28 '17 at 19:03
source share
 editText.setInputType(InputType.TYPE_NULL); 
-2
02 Sep '15 at 10:47
source share



All Articles