I'm having an EditText
and a Button
in my layout. After writing inside the edit field and clicking on the Button
, I want to hide the virtual keyboard. I guess there should be a simple, one- or two-liner to make this happen. Where can I find an example of it?
views:
17680answers:
5You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow
, passing in the token of the window containing your edit field.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.HIDE_IMPLICIT_ONLY
as the second parameter to ensure you only hide the keyboard when the user didn't explicitly force it to appear (by holding down menu).
Please try this below code in oncreate
()
EditText edtView=(EditText)findViewById(R.id.editTextConvertValue);
edtView.setInputType(0);
Also useful for hiding the soft keyboard is:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
This can be used to suppress the keyboard until the user actually touched the edittext view.
Meier's solution works for me too. In my case the top level of my App is a tabHost and I want to hide the keyword when switching tabs - I get the window token from the tabHost View.
tabHost.setOnTabChangedListener(new OnTabChangeListener()
{
public void onTabChanged(String tabId)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);
}
}
I tried all the above steps but nothing worked for me.What I am trying to do is close soft keyboard & as well finish the activity.( http://stackoverflow.com/questions/3940127/intercept-back-button-from-soft-keyboard/3994485).
I found that it is not the onKeyDown action that is invoked when clicking Back when Soft Keyboard is On. Could you please guide me what action is performed at the mentioned moment.