Android TabWidget detect click on current tab

Solution 1:

After gothrough many solutions for tab listener, I have found very simple solution...

getTabHost().setOnTabChangedListener(new OnTabChangeListener() {

@Override
public void onTabChanged(String tabId) {

int i = getTabHost().getCurrentTab();
 Log.i("@@@@@@@@ ANN CLICK TAB NUMBER", "------" + i);

    if (i == 0) {
            Log.i("@@@@@@@@@@ Inside onClick tab 0", "onClick tab");

    }
    else if (i ==1) {
            Log.i("@@@@@@@@@@ Inside onClick tab 1", "onClick tab");
    }

  }
});

Solution 2:

After a lot of thinking about this, the solution ended up being easier than I thought. What I did was just create a new child class that extends TabHost and override the setCurrentTab method like so:

package com.mycompany.Views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TabHost;
import android.widget.Toast;

public class ReclickableTabHost extends TabHost {

    public ReclickableTabHost(Context context) {
        super(context);
    }

    public ReclickableTabHost(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setCurrentTab(int index) {
        if (index == getCurrentTab()) {
            // FIRE OFF NEW LISTENER
        } else {
            super.setCurrentTab(index);
        }
    }
}

To use your new class instead of the typical TabHost just edit your layout xml file with:

<FrameLayout
    android:layout_height="match_parent"
    android:layout_width="0dip"
    android:layout_weight=".8">

    <com.myCompany.Views.ReclickableTabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/tab_unselected_holo"/>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            </FrameLayout>

        </LinearLayout>

    </com.myCompany.Views.ReclickableTabHost>

Hope this helps...

Solution 3:

I think I have found a solution, here follows a sample code:

    intent = new Intent(this, HomeGroup.class);
    View tab1 = _inflater.inflate(R.layout.custom_tab_1,null);
    homeTab.setTag("Tab1");
    spec = tabHost.newTabSpec("Tab1").setIndicator(tab1).setContent(intent);
    tabHost.addTab(spec);

    View tab2 = _inflater.inflate(R.layout.custom_tab_2,null);
    homeTab.setTag("Tab2");
    spec = tabHost.newTabSpec("Tab2").setIndicator(tab2).setContent(intent);
    tabHost.addTab(spec);

    View tab3 = _inflater.inflate(R.layout.custom_tab_3,null);
    homeTab.setTag("Tab3");
    spec = tabHost.newTabSpec("Tab3").setIndicator(tab3).setContent(intent);
    tabHost.addTab(spec);

    tabHost.setOnTabChangedListener(this);

    //click on seleccted tab
    int numberOfTabs = tabHost.getTabWidget().getChildCount();
    for(int t=0; t<numberOfTabs; t++){
        tabHost.getTabWidget().getChildAt(t).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_UP){

                    String currentSelectedTag = MainTab.this.getTabHost().getCurrentTabTag();
                    String currentTag = (String)v.getTag();
                    Log.d(this.getClass().getSimpleName(), "currentSelectedTag: " + currentSelectedTag + " currentTag: " + currentTag);
                    if(currentSelectedTag.equalsIgnoreCase(currentTag)){
                        MainTab.this.getTabHost().setCurrentTabByTag(currentTag);
                        String newSelectedTabTag = MainTab.this.getTabHost().getCurrentTabTag();
                        if(newSelectedTabTag.toLowerCase().indexOf("tab1")!=-1){
                            //do smthg
                        }else if(newSelectedTabTag.toLowerCase().indexOf("tab1")!=-1){
                            //do smthg
                        }else if(newSelectedTabTag.toLowerCase().indexOf("tab3")!=-1){
                            //do smthg
                        }
                        return true;
                    }
                }
                return false;
            }
        });
    }       

Probably it is possible to improve it, but this does the work for me!

Solution 4:

Ugly fix:IN the onClickListener put : tabHost.SetCurrentTab(OtherTab); tabHost.SetCurrentTab(CurrentTab); Where for index of Other Tab I use my simplest view under the tabs.

P.S. Customers always want their apps to be different :)

This is the code that I use (I have only 2 tabs Tab1 and Tab2):

 getTabWidget().getChildAt(1).setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 

                Log.d(TAG,"1"+getTabHost().getCurrentTabTag());

                if (getTabHost().getCurrentTabTag().equals("Tab2")) { 
                    Log.d(TAG,"2");

                    tabHost.setCurrentTab(0);                                    
                    tabHost.setCurrentTab(1);

                } else {
                    tabHost.setCurrentTab(1);
                }
            } 
        });

Solution 5:

The problem here is that setOnTabChangedListener does not fire when clicking on the selected tab, and if you set an OnClickListener on the tab, you lose the normal tab behavior.

So an easy solution is to put OnClickListener on the tab, and inside it, set programatically that this is the current tab.

With TabHost:

getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // custom code
        tabHost.setCurrentTab(0);                                    
    }
});

With TabLayout

tabLayout.getTabAt(0)setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    // custom code
                    TabLayout.Tab tab  = tabLayout.getTabAt(0);
                    tab.select();
                }
            }
        });