I am trying to hide keyboard after selecting an item from Spinner
but the code is not working and nothing happens. But in other side the same code works in normal fragment.
Here is the method to hide keyboard:
public static void hideKeypad(Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
You need to specify the rootView
of the Fragment
because in your method it's see the getCurrentFocus() == null
, So it will never go to the rest of the code.
This is the right code:
public static void hideKeypad(Activity activity, View view) {
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Make a class variable View
and equal it with the rootView
of the Fragment
in the onCreateView()
and use this method anywhere in this Fragment
.