OnClickListener on Tabs not working
Asked Answered
M

4

8

Greetings,

I am trying to get the Click - event when clicking on the currently selected tab of my TabActivity. The onTabChangedHandler is only called whenever the tab is changed, not if the currently active Tab is clicked. The debugger tells me i have the onClickListener Registered for the TabWidget within my TabHost.

Am i registering for the wrong View?

Also, I am unable to create a Context Menu for the Tabs, only for its content, is this problem related?

public class TestDroidViewTab extends TabActivity 
                              implements TabContentFactory
                              , OnTabChangeListener, OnClickListener {

  private static final String LOG_KEY = "TEST";
  ListView listView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      final TabHost tabHost = getTabHost();


      TabHost.TabSpec ts = tabHost.newTabSpec("ID_1");
      ts.setIndicator("1"); 
      ts.setContent(this);
      tabHost.addTab(ts);

      ts = tabHost.newTabSpec("ID_2");
      ts.setIndicator("2"); 
      ts.setContent(this);
      tabHost.addTab(ts);

      ts = tabHost.newTabSpec("ID_3");
      ts.setIndicator("3"); 
      ts.setContent(this);
      tabHost.addTab(ts);
      tabHost.setOnClickListener(this);
      tabHost.setOnTabChangedListener(this);
  }
  public void onClick(View v) {
      Log.d(LOG_KEY, "OnClick");
  }

  public void onTabChanged(String tabId) {
      Log.d(LOG_KEY, "OnTabChanged");
  }
Martian answered 30/6, 2009 at 8:42 Comment(0)
P
19

If you want to see that a particular tab is clicked, you need to add your listener to the tab itself, not the TabHost.

The hierarchy of views in a tab implementation is:

  • TabHost
    • TabWidget
      • (tab)
      • (tab)
    • FrameLayout

The tabs are added at runtime by calling: tabHost.addTab(tabHost.newTabSpec(""));

You can then get a handle to the individual tabs by calling: getTabWidget().getChildAt(4);

In essence, you are adding your OnClickListener to a child of the TabWidget. You can now pick up the clicks on your individual tab. However, this will override the default behavior which changes the content when a tab is clicked. So, to get your content to change, your OnClickListener will need to do that for you.

Here is a full example, which lets you intercept the click event, and change the content below the tab:

final String myTabTag = "My Tab";
final int myTabIndex = 3;

getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );

getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
            getTabHost().setCurrentTab(myTabIndex );
        }
    }
});
Pylle answered 21/5, 2010 at 19:17 Comment(1)
when i try this it changes the tab but not the content below the tab ... any suggestions?Heterosporous
M
10

use setOnTabChangedListener instead of OnClickListener ;)

    static TabHost tabHost;

    tabHost = getTabHost();


    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
       @Override
      public void onTabChanged(String arg0) {
       Log.i("******Clickin Tab number ... ", "" + tabHost.getCurrentTab());
      }     
});  
Maymaya answered 5/2, 2010 at 1:41 Comment(4)
Aren't you supposed to call a super method to get the tab "clickable" as before? When I add this code it click, the mouse shines, but it does not show the listActivity on this tab.Manque
Sorry my mistake, works like a bomb, I had extra code that was taking over further down.Manque
I think there is mistake in code...Here it should be getTabHost instead of Tabhost.....Marlysmarmaduke
hi Chirag actually tabhost comes from getTabHost(); see the original post, final TabHost tabHost = getTabHost();Maymaya
A
2

Your clause is wrong, use:

...

if (getTabHost().getCurrentTabTag().equals(myTabTag) == false) {
            getTabHost().setCurrentTab(myTabIndex );
   }

...

Arlina answered 21/6, 2010 at 14:34 Comment(0)
M
0

into my code, it shows some errors and ask me to create new methods in those names like getTabWidget(), getTabHost(), etc. Waiting for your response.

Try this

 tabHost.getTabHost().setCurrentTab(myTabIndex);
Misfeasance answered 31/5, 2013 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.