0

How to hide the keyboard after moving another tab using the method in android ? My Code switch in tabs - how methond hide keybord

public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position){
            case 0:
                fragment = new Tab1Fragment();
                break;
            case 1:
                fragment = new Tab2Fragment();
                break;
            default:
                fragment = null;
                break;
        }
        return fragment;
    }
  • have you tried closing the keyboard programmatically on click on another fragment ? [Check this](https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard) – shadygoneinsane May 25 '17 at 07:22

3 Answers3

0

Put below code in your onCreate() in Fragments.

        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);// for hide keypad
Abhishek Kumar
  • 1,075
  • 10
  • 19
0

I did it using following method:

public void hideSoftKeyboard(View viewFocus) {
        // Check if no view has focus:
        if (viewFocus != null) {
            InputMethodManager imm = (InputMethodManager) viewFocus.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(viewFocus.getWindowToken(), 0);
        }
    }

In Activity/Fragment call this method like:

hideSoftKeyboard(getCurrentFocus())

Hope it help you !

Vikas Tiwari
  • 364
  • 3
  • 11
0

You can use this method to hide the keyboard:

    public static void hideKeyboard(Activity act){
        View view = act.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager)act.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

and call it where ever you initiate you TabLayout, like:

 tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
           hideKeyboard(myActivity);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
dali
  • 36
  • 1
  • 7