how to detecting a click on an already selected tab button
Asked Answered
R

3

8

I am trying to get the Click event when clicking on currently selected tab of my TabActivity.

I tried below code but when i click on one tab the other tabs are not working/clicking properly.

    setupTab(new TextView(this), "Map");
    setupTab(new TextView(this), "Attacks");
    setupTab(new TextView(this), "Profile");
    setupTab(new TextView(this), "Headquater");

    int numberOfTabs = tabHost.getTabWidget().getChildCount();
    for(int t=0; t<numberOfTabs; t++){
    getTabWidget().getChildAt(t).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(tabHost.getCurrentTab() == 0){
                    Toast.makeText(TabContext, ""+"i m in on clickkkkk" ,1500).show();
                    getTabHost().setCurrentTab(0);
                }
                if(tabHost.getCurrentTab() == 1){
                    Toast.makeText(TabContext, ""+"i m in on clickkkkk....$#@$#$" ,1500).show();
                    getTabHost().setCurrentTab(1);
                }

            }
        });
    }
Reamer answered 21/2, 2012 at 5:51 Comment(0)
R
9

I needed to detect second click only for one tab, so this answer worked for me. The trick is setting the OnClick for the child and not for the TabHost itself.

getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.i("TAG", "Clicked tab : "+getTabHost().getCurrentTabTag());
        tabHost.setCurrentTab(0);                                    
    }
});
Rosierosily answered 5/11, 2012 at 6:40 Comment(1)
This works perfectly, even for a complex tabhost/fragments setup. Thanks!Widthwise
C
3

I used theczechsensation solution but with minor modifications because adding a listener to child and not to the tab host itself will cause confusion to tabhost listener to read comments in the code for better classifications

 mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // re enforce selecting targeted tab to re apply main listener to default behaviour
                mTabHost.setCurrentTab(0);
                // display current tab tag in console
                Log.i("MainActivity", "Clicked tab : "+mTabHost.getCurrentTabTag());
                // identify targeted fragment
                Fragment currentFragment = new "FragmentClassName"();
                // execute navigation (fragment transaction)
                fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, currentFragment)
                        .commit();
                // re enforce selecting targeted tab to re apply main listener to default behaviour
                mTabHost.setCurrentTab(0);
            }
        });
Christophany answered 25/12, 2014 at 13:3 Comment(0)
R
2

Inside your onCreate method:

for(int i = 0; i < TOTAL_TAB; i++) {  
    tabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
    tabHost.getTabWidget().getChildAt(i).setTag(i+"");  
}  

your listener:
@Override

public boolean onTouch(View v, MotionEvent event) {
    if(MotionEvent.ACTION_DOWN == event.getAction()) {
        previousTab = tabHost.getCurrentTab();
        currentTab = Integer.parseInt((String)v.getTag());
        if(previousTab == currentTab) {
            if(currentTab == 0){
                Log.i("log", "0th same tab selected");
            }else if(currentTab == 1){
                Log.i("log", "1st same tab selected");
            }else if(currentTab == 2){
                Log.i("log", "2nd same tab selected");
            }else if(currentTab == 3){
                Log.i("log", "3rd same tab selected");
            }
            return true;
        }
    }
    return false;
}
Raspy answered 26/7, 2013 at 8:7 Comment(1)
and yeah TabHost tabHost = getTabHost();Raspy

© 2022 - 2024 — McMap. All rights reserved.