0

I used Tab-activity in my application,but this class is depreciated now,how can I replace this with fragment.I have implemented sub tabs also for each tabs.Can any one help me by providing sample code to implement this changes?

AbiAndroid
  • 89
  • 2
  • 4
  • 14

2 Answers2

1

Check out these links

The first has sample code on how to build tabs using fragments (you can pretty much use it as is), and the latter is a discussion about the same.

Community
  • 1
  • 1
stuckless
  • 6,515
  • 2
  • 19
  • 27
  • I tried this and tab are getting,but I want to get some data in fragment class from main class .Same as putting extra data to invoked activity,how we can done this in fragment. – AbiAndroid May 25 '12 at 13:00
  • In the FragmentTabs example, one of the arguments when adding a Fragment is a `Bundle` of arguments, which can be read by calling `getArguments()` in the Fragment. – stuckless May 26 '12 at 15:40
  • Also, keep in mind that in the Fragment, you have access to the parent Activity, so you can also read the intent from the activity, in case you need to use that data. ie, in the fragment, you call `getActivity().getIntent()`, but typically you pass arguments to a Fragment using the `Bundle` and read them use `getArguments()` – stuckless May 26 '12 at 15:44
0

Use actionbar with only tabs.ActionBarSherlock library can do this very easily. Go through TabNavigationCollapsed class in samples of ActionBarSherlock.

public class TabNavigationCollapsed extends SherlockActivity implements ActionBar.TabListener {
private TextView mSelected;

@Override
public void onCreate(Bundle savedInstanceState) {
    setTheme(SampleList.THEME); //Used for theme switching in samples
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tab_navigation);
    mSelected = (TextView)findViewById(R.id.text);

    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for (int i = 1; i <= 3; i++) {
        ActionBar.Tab tab = getSupportActionBar().newTab();
        tab.setText("Tab " + i);
        tab.setTabListener(this);
        getSupportActionBar().addTab(tab);
    }
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
    mSelected.setText("Selected: " + tab.getText());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}

}

Prabhu M
  • 3,534
  • 8
  • 48
  • 87