Vinod
Vinod

Reputation: 32861

How to get tab click in Android?

I have created two tabs in my Activity using the below code. This is working perfectly fine. When I click on the first tab I see its contents and the other one shows its content when clicked on it.

How ever I want to set a variable value to true or false based on the Tab selected. But I dont know how to get the tab click for this tab. Can you please help me on this.

The code :

tabHost.setup();

    TabSpec ts = tabHost.newTabSpec("Tab1");
    ts.setIndicator("", getResources().getDrawable(R.drawable.tab1_content));
    ts.setContent(R.id.tab1Layout);


    tabHost.addTab(ts);
    TabSpec ts1 = tabHost.newTabSpec("Tab2");
    ts1.setIndicator("", getResources().getDrawable(R.drawable.tab2_content));
    ts1.setContent(R.id.tab2Layout);
    tabHost.addTab(ts1);

Upvotes: 1

Views: 5407

Answers (1)

Rohit Sharma
Rohit Sharma

Reputation: 13815

Add onTabchangedListener to the tabhost and using selectedTab Value manage whatsoever you want to manage. selectedTab value will be = 0 for first tab and rest so on

 tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
    int selectedTab = tabHost.getCurrentTab() // selected
 }
 });

Hope it helps :)

Upvotes: 3

Related Questions