0

I have three tabs. On the second one, when I click on the edit text, the keyboard comes up. I want to hide the keyboard whenever I click on the tab1 indicator.

I already dig up the method to hide the keyboard:

InputMethodManager imm = (InputMethodManager)getSystemService(
  Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

How can force the keyboard to hide when I switch my tab?

solalito
  • 1,189
  • 4
  • 19
  • 34

1 Answers1

1

Setup the listener OnTabChangeListener with setOnTabChangedListener in your TabHost, and inside TabHost.OnTabChangeListener add your code block.

Something like this:

tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener()
{
    @Override
    public void onTabChanged(String tabId)
    {
        InputMethodManager imm = (InputMethodManager) getSystemService(
                Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

    }
});

You can use ActionBar to create tabs anyway

An example:

ActionBar actionBar = getActionBar();
actionBar.addTab(actionBar.newTab().setText("Your Tab").setTabListener(new ActionBar.TabListener()
{
    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
    {
        InputMethodManager imm = (InputMethodManager)getSystemService(
                Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
    {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
    {

    }
}));

.addTab() .newTab() .setTabListener()

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • You should change `tabHost` with the reference to your TabHost – Marco Acierno Mar 30 '14 at 17:16
  • What do you mean by deprecated? Is there any difference between TabHost and ActionBar? – solalito Mar 30 '14 at 17:29
  • Because since Android 3.0 you can use ActionBar to provide the same features in a better and if you need to use it in versions <= Android 3.0 you can use Support library. I found this, http://stackoverflow.com/questions/14272125/android-tabhost-deprecated – Marco Acierno Mar 30 '14 at 17:32