6

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
  • 18,954
  • 30
  • 85
  • 131
  • `TabHost.OnTabChangeListener` – st0le Aug 09 '10 at 06:59
  • 1
    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 Aug 15 '10 at 12:23

4 Answers4

4

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

        }
});
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
0m4r
  • 1,036
  • 1
  • 14
  • 25
  • 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? – Sudar Dec 03 '10 at 09:23
  • 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 :) http://stackoverflow.com/questions/4337514/android-tabwidget-detect-click-on-current-tab – 0m4r Dec 03 '10 at 10:21
  • 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. – Niels Dec 17 '12 at 15:32
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.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
Vikas
  • 4,263
  • 1
  • 34
  • 39
  • Do you mean the onResume() method of the activity? Also it would be helpful, if you can show a complete example. – Sudar Aug 15 '12 at 12:46
3

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

Community
  • 1
  • 1
0m4r
  • 1,036
  • 1
  • 14
  • 25
0
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        getTabWidget().getChildAt(i).setOnClickListener(new OnClickListener() { 

            @Override 
            public void onClick(View v) { 

                if (getTabHost().getCurrentTabTag().equals(v.getTag()))
                {
                    int nextTab = getTabHost().getCurrentTab();
                    tabHost.setCurrentTab(prevTab);
                    tabHost.setCurrentTab(nextTab);
                    prevTab = nextTab;
                }
                else
                    tabHost.setCurrentTabByTag((String) v.getTag());
            } 
        });
    }

You need a global variable;

    private int prevTab = 1;   //any tab except the initial one.

This code works for me. A little ugly thing is you must set same tag for tab and view For example;

    intent = new Intent().setClass(this, AnaSayfa.class);
    spec = tabHost.newTabSpec("firstTab").setIndicator(makeTabIndicator(R.drawable.firstTab, "First Tab" , "firstTab"))
                  .setContent(intent);
    tabHost.addTab(spec);

and makeTabIndicator method is like that,

    private View makeTabIndicator(int drawable, String text, String viewTag){ 

        View view = LayoutInflater.from(this).inflate(R.layout.tab_layout, null);

        ImageView image = (ImageView) view.findViewById(R.id.imageView1);       
        image.setImageResource(drawable);
        image.setAdjustViewBounds(true);

        TextView tv = (TextView) view.findViewById(R.id.textView1);
        tv.setText(text);

        view.setTag(viewTag);               
        return view;
    }
efeyc
  • 2,122
  • 2
  • 27
  • 39