Capturing the tab click event in Android Tabview

30,544

Solution 1

If you ares till looking for a solution, I might have found one. Take a look here: Android TabWidget detect click on current tab

Solution 2

This is how your code should work :

getTabWidget().getChildAt(getTabHost().getCurrentTab()).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        //do whatever you need

        }
});

Solution 3

I found one clean and easy solution for Detecting clicks on selected Tab

Steps:

1: Extend TabActivity in your class. 2: In the onResume() method implement the following method

For each tab (i) implement this:

TabHost tabHost = getTabHost();

public void onResume() {

 super.onResume();
     tabHost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                   count++;
                   tabHost.setCurrentTab(0);
    //based on your count value..you can do anything...like going back to homepage...
    //    similar thing exist on iphone (double tab a tab..it takes back to homepage)
     }
   });
 }      

Since we always have a fixed number of tabs, implementing it separately is not a problem.

Share:
30,544

Related videos on Youtube

Sudar
Author by

Sudar

I am a developer from Chennai, India, mostly interested in WordPress, Android and Arduino programming. I write about my projects at my blog. You can also checkout my code that I have released as open source in my github account. You can follow me on twitter.

Updated on July 09, 2022

Comments

  • Sudar
    Sudar almost 2 years

    I have a tabview in my android app with 3 tabs. The tabs are all working fine.

    Now I want to perform some additional logic when the tab (on the top) of the currently active tab is clicked.

    Here is an example:

    In one of my tabs, I provide an option for the user to sort things in different order. When the press the tab of the currently active tab, I want to reset all these sorting.

    Is it possible to capture the tab click event in tabview and perform some additional logic?

    Edit: Edited for clarity.

    • Sudar
      Sudar over 13 years
      TabHost.onTabChangeListener is getting fired only when the tab is changed. But I want the event to be fired, even if the tab header of the currently active tab is clicked. Is it possible to do that?
  • Sudar
    Sudar over 13 years
    Thanks, this code works. But it is introducing a new problem. Now the tabs are not getting switched when they are clicked. Do I have to call any additional methods to make the tabs change?
  • 0m4r
    0m4r over 13 years
    I am working to solve this issue as well... so far I do not have an answer if not to implement you own "switching" code. If you succeed please share :) stackoverflow.com/questions/4337514/…
  • Sudar
    Sudar over 11 years
    Do you mean the onResume() method of the activity? Also it would be helpful, if you can show a complete example.
  • Niels
    Niels over 11 years
    Vikas shows a fix to make the tabs switch. Inside the listener add: getTabHost().setCurrentTab(getTabHost().getCurrentTab()); Or if the getCurrentTab() gives trouble, create a final int with the tab's index outside the listener, and use that instead.

Related