Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 708
CodePrimate Avatar asked Apr 24 '12 11:04

CodePrimate


People also ask

How do I hide my soft keyboard?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I hide the soft keyboard on Android after clicking outside Edittext?

and put the following code in the onTouch method. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I hide my keyboard when typing?

Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden.


2 Answers

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);
    }
like image 140
jclafuente Avatar answered Sep 22 '22 06:09

jclafuente


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();
like image 20
Jaz Avatar answered Sep 23 '22 06:09

Jaz