Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index of selected tab in tabHost

I'm trying to store the index of the currently selected tab in onSaveInstanceState so I can restore it. However the getCurrentTab apparantely gives me back the String I used in the etTabHost().newTabSpec, which I find a bit weird since the documentation says it returns an int and setCurrentTab also taking an int. Does anyone know how I can get the index of my currently selected tab so I can restore it?

like image 650
Jack Avatar asked Aug 27 '10 10:08

Jack


3 Answers

you are on the right way, use setOnTabChangedListener to get your selected tab.

public class MainActivity extends TabActivity {
    static TabHost mytabs;

    mytabs = getTabHost();
    mytabs.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String arg0) {         
            Log.i("***Selected Tab", "Im currently in tab with index::" + mytabs.getCurrentTab());
        }       
    });  
...
...
...
like image 158
Jorgesys Avatar answered Nov 17 '22 23:11

Jorgesys


You can use getCurrentTab() that returns index of tab start from 0.

like image 20
Pranav Avatar answered Nov 18 '22 00:11

Pranav


Use tabHost.getCurrentTab() to get Tab ...

tabHost= getTabHost();
tabHost.addTab(tab0); // TabSpec tab0=tabHost.newTabSpec(...
tabHost.addTab(tab1); //  TabSpec tab1=tabHost.newTabSpec

int current = tabHost.getTabHost() ;
like image 38
Mi.HTR Avatar answered Nov 17 '22 23:11

Mi.HTR