Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detecting a click on an already selected tab button

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);
                }

            }
        });
    }
like image 663
Kutbi Avatar asked Feb 21 '12 05:02

Kutbi


Video Answer


2 Answers

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);                                    
    }
});
like image 105
theczechsensation Avatar answered Sep 20 '22 15:09

theczechsensation


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);
            }
        });
like image 44
Zainab Mohamed Avatar answered Sep 19 '22 15:09

Zainab Mohamed