3

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);

5 Answers 5

8
+50

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);
            }
        }
    }
2
  • thanks,this worked for me in tab fragments when switching
    – Meerz
    Aug 17, 2018 at 6:59
  • 1
    setUserVisibleHint() has been deprecated for a while now. You may want to simply move the logic into onResume() instead Nov 5, 2019 at 3:24
3

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);
    }
}
1
  • Answers a question not asked.
    – Mitch
    Oct 23, 2019 at 3:34
1

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) {

    }
0

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);
        }
    }
7
  • I have tried here:public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView=inflater.inflate(R.layout.fragment_route_audit, container, false); hideKeyboard(rootView); ((SideDrawerActivity)getActivity()).setTitle("Route Audit"); BindView(rootView); return rootView; }
    – Ravi Yadav
    Feb 6, 2015 at 5:23
  • @user3138859 - try to hide the keyboard in onDestroyView of every fragment
    – user543
    Feb 6, 2015 at 5:33
  • is the keyboard is enabled when the fragment is Visible?
    – Ramesh
    Feb 6, 2015 at 5:43
  • @user3138859 -when the keyboard is visible in ur situation?
    – user543
    Feb 6, 2015 at 5:46
  • when i enter value in edittext then keyboard is open... and then suppose I move to another tab ...second fragment is being called.. even then keyboard is still opened
    – Ravi Yadav
    Feb 6, 2015 at 6:09
0

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...

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.