androidandroid-fragmentsandroid-tabhost

How to switch tabs programmatically in Android from fragment?


I have implemented a TabActivity which extends FragmentActivity. It has 5 tabs each tab is a Fragment. What i am looking for is to switch between the tabs programmatically.

For eg: if i am in tab4. On button click I want to move from tab4 to tab1. Tried a lot but couldn't find the solution for this.

Tried with the following but it doesn't help.

From SecondTab

public void switchTabInActivity(String value){
    FirstTab parent;
    parent = (FirstTab) getActivity().getParent();
    parent.switchTab(value);
}

TabActivity

  /** To Change Tab*/
public void switchTab(String tabno){ 
    this.onTabChanged(tabno);
}

Solution

  • Finally i can switch between the tabs programatically from Fragments using the following line of code

      TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
      host.setCurrentTab(2);
    

    Hope it will help some one.