'Hide keyboard when switching tabs in android
I have created tab strip with custom classes and I am displaying one fragment in each tab. When the keyboard is open and I switch to tab then second fragment is getting called but the keyboard is not hiding.
I am using the code below in onCreateView() in both fragment but it's not working:
//To Hide Soft
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Solution 1:[1]
The problem with using that code in onCreateView()
is the fragment initialized within the tabs are created whenever the tabs are created in the parent fragment / activity. I did some investigating with the behavior of fragments within tabs and realised you'd have the same problem overriding many of the lifecycle methods such as onViewCreated()
, onResume()
, etc.
I found that the best solution to this problem is to override setUserVisibleHint(boolean isVisibleToUser)
in the fragment where you want the keyboard to be hidden. This method is called any time the visibility of a fragment changes.
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
try {
InputMethodManager mImm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
mImm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
Log.e(TAG, "setUserVisibleHint: ", e);
}
}
}
Solution 2:[2]
Use this class to hide and show keyboard at runtime. Try to call the method on your onTabChangedListener. Hope it helps.
public class KeyBoardHandler {
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
public static void showSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
Solution 3:[3]
Put this code in onDestroy method of fragment.
try {
InputMethodManager mImm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
mImm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}
Solution 4:[4]
Try with below code
Interface: ICallBacks
public interface ICallBacks {
public void isChanged();
}
In your activity define on variable like
public ICallBacks mCallbacks;
In OnPageChangeListener
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
if (mCallbacks != null)
mCallbacks.isChanged();
}
In your fragment you need to implement with ICallBacks interface
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
if (activity != null) {
((PagerActivity) getActivity()).mCallbacks = this;
}
}
@Override
public void isChanged() {
if (isVisible())
hideKeyboard();
}
private void hideKeyboard() {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
View view = this.getCurrentFocus();
if (view != null) {
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Solution 5:[5]
If you come here like me in 2022 you should be using ViewPager2
and onResume
and onPause
methods to know which fragment is currently visible.
I found this answer here and I got to it thanks to the IDE telling me a method used in this answer is deprecated, so thanks for the answer!
I am still trying to figure out how to not show it in the first place, but I guess that's because I selected something on the other tab which for some reason gets loaded first...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mark O'Sullivan |
Solution 2 | icaneatclouds |
Solution 3 | AmmY |
Solution 4 | |
Solution 5 | Chagai Friedlander |