2

I have implemented ActionBar Tab in my app. But I am facing one issue during tab change. My tabs contains mainly webview, but one tab contains edit text. when i click on edit text keyboard appears, and with keyboard appearing if I am changing the tab, keyboard is not disappearing. I tried few of the simple solution like hiding it explicitly, but no success.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 mgr.hideSoftInputFromWindow(fragment.getView().getApplicationWindowToken(), 0);

this i am calling in onTabSelected() of class that implements ActionBar.TabListener. I don't know how to solve this problem , neither getting relevant information.

Thanks in advance. Any help will appreciated.

Update and Answer

Eric answer somewhat gave me a push and helped me achieve the answer, so i am marking his answer as correct with my change. ie I have added the eric's code in my onTabUnselected but not in tabSelected, as when i was trying to get view at that moment view was not created thus was getting view as null. so my final code was

@Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft)
    {
        View target = initialisedFragment.getView().findFocus();

        if (target != null) 
        {
            InputMethodManager mgr = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.hideSoftInputFromWindow(target.getWindowToken(), 0);
        }
    }
Android
  • 3,660
  • 8
  • 41
  • 76

2 Answers2

2

I don't believe you can just pick a View and use it as the window token. You have to find the field that is currently showing the keyboard.

This is a port of a method I've used before, it's worth a try:

View target = fragment.getView().findFocus();
if (target != null) {
    InputMethodManager imm = (InputMethodManager) target.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
}

If that doesn't work, there's lots of other methods that have been reported to work.

Community
  • 1
  • 1
Eric
  • 63,873
  • 22
  • 120
  • 135
  • ohh, k. I was not getting a view, because usually we pass tabHost to this view, but in action bar there was no tab host . So i preferred to pass this view. thanks for the reply. I am trying this out. Thanks again. – Android Jul 26 '12 at 06:07
  • this is not working and even your link is passing a editText as view. but here i don't have any view as such because fragment.getView() is always passed as null. even adding alwaysHidden in manifest is also not working. Don't know how to proceed.... – Android Jul 26 '12 at 06:13
  • You need to figure out whatever has the focus. Perhaps using `getActivity()` or `getRootView()` of a view you do have, then finding the focus of that? If `getView()` is null then your `Fragment` probably either isn't laid out or isn't instantiated properly... – Eric Jul 26 '12 at 17:35
  • Thanks I found out my mistake and updated my question with my solution and since u helped me out in achieving this I marked your answer as correct instead of creating one. Thanks. – Android Jul 27 '12 at 04:37
0

I use the following, this option to capture the current view in which the device is working :

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);
        }
    }
jclafuente
  • 193
  • 1
  • 5