Nader
Nader

Reputation: 39

Closing soft keyboard after switching tabs in tabhost

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?

Upvotes: 3

Views: 1817

Answers (1)

Evos
Evos

Reputation: 3915

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

Upvotes: 6

Related Questions