I have an EditText
and a Button
in my layout. After writing in the edit field and clicking on the Button
, I want to hide the virtual keyboard. I assume that there's a simple, one- or two-liner to make this happen. Where can I find an example of it?
Source: Tips4all
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.
ReplyDeleteInputMethodManager 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).
Also useful for hiding the soft keyboard is:
ReplyDeletegetWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
This can be used to suppress the keyboard until the user actually touched the edittext view.
Please try this below code in oncreate()
ReplyDeleteEditText edtView=(EditText)findViewById(R.id.editTextConvertValue);
edtView.setInputType(0);
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.
ReplyDeletetabHost.setOnTabChangedListener(new OnTabChangeListener()
{
public void onTabChanged(String tabId)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);
}
}
Simplest way:
ReplyDelete//Show soft-keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//hide keyboard :
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You must use the following code to hide the soft keyboard :
ReplyDeleteInputMethodManager inputManager = (InputMethodManager)
Context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
Hi i got one more solution to hide keyboard by :
ReplyDeleteInputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
Here pass HIDE_IMPLICIT_ONLY at the position of showFlag and 0 at the position of hiddenFlag. It will forcefully close soft Keyboard.
from so searching, here I found an answer that works for me
ReplyDelete// Show soft-keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
// Hide soft-keyboard:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
If all the other answers here don't work for you as you would like them to, there's another way of manually controlling the keyboard.
ReplyDeleteCreate a function with that will manage some of the EditText's properties:
public void setEditTextFocus(boolean isFocused)
{
searchEditText.setCursorVisible(isFocused);
searchEditText.setFocusable(isFocused);
searchEditText.setFocusableInTouchMode(isFocused);
if (isFocused)
{
searchEditText.requestFocus();
}
}
Then, make sure that onFocus of the EditText you open/close the keyboard:
searchEditText.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (v == searchEditText)
{
if (hasFocus)
{
//open keyboard
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(searchEditText,
InputMethodManager.SHOW_FORCED);
}
else
{ //close keyboard
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
searchEditText.getWindowToken(), 0);
}
}
}
});
now, whenever you want to open the keyboard manually call:
setEditTextFocus(true);
And for closing call:
setEditTextFocus(false);
For force show and hide we need to use http://android-codes-examples.blogspot.com/2011/11/show-or-hide-soft-keyboard-on-opening.html
ReplyDeleteas shown on this blog
If you want to close the soft keyboard during a unit or functional test, you can do so by clicking the "back button" from your test:
ReplyDelete// Close the soft keyboard from a Test
getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
I put "back button" in quotes, since the above doesn't trigger the onBackPressed() for the Activity in question. It just closes the keyboard.
Make sure to pause for a little while before moving on, since it takes a little while to close the back button, so subsequent clicks to Views, etc., won't be registered until after a short pause (1 second is long enough ime).
protected void hideSoftKeyboard(EditText input) {
ReplyDeleteinput.setInputType(0);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
Here's how you do it in Mono for Android (AKA MonoDroid)
ReplyDeleteInputMethodManager imm = GetSystemService (Context.InputMethodService) as InputMethodManager;
if (imm != null)
imm.HideSoftInputFromWindow (searchbox.WindowToken , 0);
I'm using a custom keyboard to input an Hex number so I can't have the IMM keyboard show up...
ReplyDeleteIn v3.2.4_r1 setSoftInputShownOnFocus(boolean show) was added to control weather or not to display the keyboard when a TextView gets focus, but its still hidden so reflection must be used:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
try {
Method method = TextView.class.getMethod("setSoftInputShownOnFocus", boolean.class);
method.invoke(mEditText, false);
} catch (Exception e) {
// Fallback to the second method
}
}
For older versions, I got very good results (but far from perfect) with a OnGlobalLayoutListener, added with the aid of a ViewTreeObserver from my root view and then checking if the keyboard is shown like this:
@Override
public void onGlobalLayout() {
Configuration config = getResources().getConfiguration();
// Dont allow the default keyboard to show up
if (config.keyboardHidden != Configuration.KEYBOARDHIDDEN_YES) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mRootView.getWindowToken(), 0);
}
}
This last solution may show the keyboard for a split second and messes with the selection handles.
When in the keyboard enters full screen, onGlobalLayout isn't called. To avoid that, use TextView#setImeOptions(int) or in the TextView XML declaration:
android:imeOptions="actionNone|actionUnspecified|flagNoFullscreen|flagNoExtractUi"
Suppose u have edit textbox with id edsearch
ReplyDeleteand button with id btnsearch
btnsearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
imm.hideSoftInputFromWindow(edSearch.getWindowToken(), 0);
imgCancel.setVisibility(View.GONE);
edSearch.setText(""); } });