How do I hide the soft keyboard when changing tabs?
Asked Answered
D

5

6

EDIT : Seems I'm not making myself clear. What I need is a way to hide the soft keyboard whenever i replace the fragment I am in. How do I go about doing this ?

Let me keep this simple. I have an EditText box in Tab Fragment 1.2 which obviously opens op the Soft keyboard when pressed. How do I hide this when the tab is changed? I tried the following in my onTabSelected() which doesn't seem to do anything

getWindow().setSoftInputMode(
                  WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

I've tried everything now. None of the suggested solutions I've located so far are helping me in any way.

Dumbstruck answered 24/4, 2012 at 11:57 Comment(2)
add android:windowSoftInputMode="stateHidden|adjustResize"> in your ActivtitySarnoff
This does nothing in my application.Dumbstruck
N
12

programmatically you could use, capturing the view of the active activity on the screen of the device.

public final void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }
public final void onTabselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }
public final void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }

private void hiddenKeyboard(View v) {
        InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        keyboard.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
Norahnorbert answered 28/1, 2014 at 10:12 Comment(0)
R
6

I had the same issue and placed the following code in my tab fragment just prior to using the FragmentTransaction.replace() method to change tabs. The onCreate and onCreateView methods in each fragment are not triggered after the initial tab selection, so hiding the keyboard can be done before getting to the specific fragment's class. Using mTabHost.getApplicationWindowToken() rather than editText.getWindowToken() was a major help. Thanks to whoever found that. Sorry I lost the link.

InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);             
im.hideSoftInputFromWindow(mTabHost.getApplicationWindowToken(), 0);

.......
fm = getFragmentManager();
....

fm.beginTransaction()
    .replace(placeholder, new someFragment(), tabId)
    .commit();
Resignation answered 13/7, 2012 at 19:12 Comment(0)
W
2

This is how you enable the soft keyboard

inputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

And this is how you close it when you switch tabs.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Whitman answered 24/4, 2012 at 12:3 Comment(0)
G
2

set OnPageChangeListener to viewpager in mainActivity Where you add fragments to viewPager

  viewPager.setOnPageChangeListener(myOnPageChangeListener);




   ViewPager.OnPageChangeListener myOnPageChangeListener =
            new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int state) {
                //Called when the scroll state changes.

            }

            @Override
            public void onPageScrolled(int position,
                                       float positionOffset, int positionOffsetPixels) {
                //This method will be invoked when the current page is scrolled,
                //either as part of a programmatically initiated smooth scroll
                //or a user initiated touch scroll.

                hideKeyboard();
            }

            @Override
            public void onPageSelected(int position) {
                //This method will be invoked when a new page becomes selected.
                //hide keyboard when any fragment of this class has been detached
                hideKeyboard();
            }
        };







public  void hideKeyboard() {
    InputMethodManager inputManager = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);

    // check if no view has focus:
    View v = getCurrentFocus();
    if (v == null)
        return;

    inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
Goulder answered 16/8, 2016 at 6:52 Comment(0)
A
-2

In your XML file just remove android:focusable="true" from everywhere.

One more thing is if you are using <requestfocus></requestfocus> then also remove this line.

Try it I think it should work.

Asuncion answered 24/4, 2012 at 12:14 Comment(2)
I failed to explain myself properly. The problem isn't the keyboard being shown when a person focuses the EditText View. It's that the soft keyboard doesn't hide once I change to a different tab.Dumbstruck
I think you have set android:focusable="false" this will not allow to open keyboard at all. I think you want to display keyboard only when user click on edittext so best way to remove above line from every where you are using.Asuncion

© 2022 - 2024 — McMap. All rights reserved.