0

I'm developing an Android App and have already written some code, but than I recognized that it would be nice to have tabs to switch between some screens.

I used the vogella tutorial for "Navigation Tabs" from: http://www.vogella.com/tutorials/AndroidActionBar/article.html#actionbar_navigation_tab

Now I have he problem that I have no clue how to add my content to the single tabs.

Also tried other tab varieties, but that confused me even more. Even the tab tutorial on android dev site was not really a better way :(

As a newbie in androidprogramming I would need now some help :)

thx for your answers

Fraggles

Fraggles
  • 463
  • 4
  • 20
  • due to my company rules I can't post my entire code :I I'm looking for something based on the tutorial as a general solution – Fraggles Jan 10 '14 at 08:19
  • setTabListener() is the method for changing the content on each tab click. Add a listener to each tab and then launch the fragment on tab click. – Mandar Kakade Jan 10 '14 at 09:29
  • I already implemented a TabListener, and it works on every tab and wirtes a number (1,2,3). – Fraggles Jan 10 '14 at 10:08
  • Has the UI of my main activity to be empty? – Fraggles Jan 10 '14 at 10:08
  • No but you must have the id of your layout in which the fragment will load. It can be a frame layout and you can replace it with a fragment on each tab click. FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.your_frame_layout, your_fragment); ft.addToBackStack(null); ft.commit(); P.S. Each fragment will have a layout of its own. – Mandar Kakade Jan 10 '14 at 10:20
  • Tried a bit, but I only get the fragment UI overlayed by the "main UI" (some buttons and textviews). But thx for your help so far. Its something I have to deal with on Monday :) – Fraggles Jan 10 '14 at 10:58
  • Ok...see if this is of any help to you http://stackoverflow.com/questions/5293850/fragment-duplication-on-fragment-transaction – Mandar Kakade Jan 10 '14 at 11:14
  • Okay, it works now. Just was to stupid to return the layout as view ... only returned the textview. Can't work :) But thx for your help guys :) – Fraggles Jan 13 '14 at 10:22

1 Answers1

1

The content of the tabs are managed by Vogella in different fragments. So what you need to do is creating different fragments for the different tabs. This fragments are then placed inside a container as soon as the tab is clicked with the help of the FragmentManager. Vogella is using the following listener to do so:

  @Override
  public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, show the tab contents in the
    // container view.
    Fragment fragment = new DummySectionFragment();
    Bundle args = new Bundle();
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1);
    fragment.setArguments(args);
    getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
  }
Ingo Schwarz
  • 607
  • 5
  • 15