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);
Mark O'Sullivan
  • 10,138
  • 6
  • 39
  • 60
Ravi Yadav
  • 2,296
  • 3
  • 25
  • 32

5 Answers5

8

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);
            }
        }
    }
Mark O'Sullivan
  • 10,138
  • 6
  • 39
  • 60
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);
    }
}
icaneatclouds
  • 1,170
  • 9
  • 18
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) {

    }
AmmY
  • 1,821
  • 1
  • 20
  • 31
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);
        }
    }
Ramesh
  • 526
  • 3
  • 14
  • 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 06 '15 at 05:23
  • @user3138859 - try to hide the keyboard in onDestroyView of every fragment – user543 Feb 06 '15 at 05:33
  • is the keyboard is enabled when the fragment is Visible? – Ramesh Feb 06 '15 at 05:43
  • @user3138859 -when the keyboard is visible in ur situation? – user543 Feb 06 '15 at 05: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 06 '15 at 06:09
  • are you using view pager? – Ramesh Feb 06 '15 at 06:23
  • i think you need to implement OnPageChangeListener in onPageScrolled you need to communicate with your fagment using with one interface in that method you nedd to call hide key board .its working for me if you are intrested then i will be post that code – Ramesh Feb 06 '15 at 06:45
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...