Reputation: 11
I am using a FrameLayout
to show an EditText
and a ListView
(with checkboxes) alternately. When showing an EditText
, I would like the soft keyboard to be shown. And when showing the ListView
, I would like the soft keyboard to be hidden. Now usually a focus is needed in order to hide the soft keyboard. When my ListView
gets shown, then getCurrentFocus()
returns null
. Is there a way to hide the soft keyboard, without having a focus?
I am showing the soft keyboard like that:
public static void requestFocusAndMoveCursorToTheEndAndShowKeyboard(final EditText editTextParam, final Activity activityParam) {
if (editTextParam == null) {
return;
}
if (editTextParam.requestFocus()) {
editTextParam.setSelection(editTextParam.getText().length()); // move Cursor to the end of the EditText
InputMethodManager imm = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
And I am trying to hide the soft keyboard like that:
public static void hideSoftInputKeyboardFromWindow(Activity activityParam) {
if (activityParam == null) {
return;
}
View view = activityParam.getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Upvotes: 0
Views: 2083
Reputation: 11
Thank you for your answers. Finally I got it solved using a View.OnFocusChangeListener for the EditText, like described here:
Hide soft keyboard on losing focus
Upvotes: 0
Reputation: 2691
How do you show them alternately? Using different fragments? Or do you simply inflate different layouts? Provide more details with your complete code
Upvotes: 0
Reputation: 1319
In your hideSoftInputKeyboardFromWindow
method, try:
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
instead of
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Edit: ok same answer as Dorami
Upvotes: 0
Reputation: 1684
Try this: Write following method to youractivity or to your utility class
/**
* Hide soft keypad
*
*/
public static void hideKeyboard(Activity activity, View v) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
Upvotes: 0
Reputation: 7065
In your AndroidMenifest.xml add this:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
Upvotes: 1
Reputation: 12358
Make use of .clearFocus(); on edittext when you don't want a focus on it.
Upvotes: 2