3

I am currently trying to make my program minimize the soft keyboard on changing of tabs. Unfortunately, I cannot find any methods provided by TabHost or otherwise to check when a tab gets changed or to run a method when a tab gets changed. I also tried adding android:onClick="hideKeyboard" with hideKeyboard being a method that closes the keyboard, but this method seems to do nothing on tab changes. The code for hideKeyboard is as follows:

public void hideKeyboard()
{
    InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}

Would there be any other methods I could try to detect a tab change? Or is my hideKeyboard() method flawed?

Nader
  • 39
  • 5
  • check this.. http://stackoverflow.com/questions/4337514/android-tabwidget-detect-click-on-current-tab – baldguy Feb 19 '13 at 04:56

1 Answers1

6

I think OnTabChangeListener is the best place to perform such operation. In your case it would be something like this:

tabhost.setOnTabChangedListener(new OnTabChangeListener(){
    @Override
    public void onTabChanged(String tabId){
        hideKeyboard()
    }
})
Evos
  • 3,915
  • 2
  • 18
  • 21
  • I tried that before but I think I did it outside of the onCreate method. Still though, thanks so much for the help :D Edit: Actually I just realized that this only toggles the keyboard and because I have three tabs, this method won't work – Nader Feb 20 '13 at 05:36
  • 1
    @Nader well you could try use `hide` command instead of `toggle`. Check this link https://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html#hideSoftInputFromWindow(android.os.IBinder, int), there are several method for hiding soft input keyboard. – Evos Feb 20 '13 at 06:46