Reputation: 300
I have a fragment which i use to show map. From this fragment I am opening another dialog fragment which have an editText. On clicking editText the keyboard opens but when I dismiss the dialogFragment without first closing the keyboard, the dialogFragment closes as it should but the keyboard remains open. and after again touching anywhere the keyboard closes. How do I close the keyboard on dismissing the dialogFragment.
I have already tried :
android:windowSoftInputMode="stateAlwaysHidden"
in activity.
also tried :
InputMethodManager imm =
(InputMethodManager) messageEditTxt.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive())
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
in onDismiss function.
Upvotes: 3
Views: 767
Reputation: 9863
I had the same problem and found that it is related to the windowSoftInput defined in the manifest. After removing android:windowSoftInputMode="stateHidden"
from the activity, it worked.
Upvotes: 1
Reputation: 1445
Just place this in your global class and you can access anywhere
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
for Activity-:
GlobalClass.hideSoftKeyborad(MainActivity.this);
for fragments-
GlobalClass.hideSoftKeyborad(getActivity);
Upvotes: 0
Reputation: 1594
Try this
public static void hideSoftKeyboard(Context context, View view) {
try {
InputMethodManager inputMethodManager =
(InputMethodManager) context.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
view.getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
Usage
hideSoftKeyboard(getActivity(), getView())
Upvotes: 1