CodePrimate CodePrimate - 2 months ago 13
Android Question

How do I hide the soft keyboard when changing tabs?

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.

Answer

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