1

I'm currently coding an application that uses a lot of Fragments accessible using a Navigation Drawer. So far so good, but I also want to have a TabHost with 2 Tabs inside one of the Fragments. How do I best implement it? This is a code snippet:

public class SectionFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

int position = getArguments().getInt("position");

if (position == 0) {

    rootView = inflater.inflate(R.layout.startmenu_layout, container,
        false); // die rootView zum Weiterarbeiten holen
} else if (position == 1) {
    rootView = inflater.inflate(R.layout.startmenu_layout, container,
        false);

and so on...

How do I proceed best? Thanks in advance,

forumfresser

forumfresser
  • 461
  • 2
  • 6
  • 15
  • How did you tried to implement it until now? – user Dec 30 '13 at 20:04
  • @Luksprog I tried calling 2 Inner Fragments (I think this is the way you should do it) and making them accessible using a TabHost, but I have no idea how to code that properly. – forumfresser Dec 30 '13 at 20:10
  • There isn't something special about what you want to do. You would use the same code just that you need to use `getChildFragmentManager()` instead of `getFragmentManager()` (or the support version). Also, you could have a look at `FragmentTabHost`. – user Dec 30 '13 at 20:32

1 Answers1

0

here is a code i use for my TabHost i know how you feel cause it's really hard to find a working tutorial and a working example out there...

anyway

TabHost tabs=(TabHost)findViewById(R.id.tabhost);

tabs.setup();

TabHost.TabSpec spec=tabs.newTabSpec("tag1");

spec.setContent(R.id.tab1);//here you define which tab you want to setup
spec.setIndicator("So Close");//here you choose the text showed in the tab
tabs.addTab(spec);

spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Contacts");
tabs.addTab(spec);

and here is the xml code I use

<TabHost
        android:id="@+id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/setting" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="46dp"
            android:background="@drawable/transparent_white" />

        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:id="@+id/tab1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="@color/white"
                android:textAppearance="?android:attr/textAppearanceLarge" />


            <TextView
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge"  />

        </FrameLayout>

    </LinearLayout>
</TabHost>

make sure you change the background of the TabWidget according to your res

you can put the xml code wherever you want in your layout file, a RelativeLayout or a LinearLayout... and you can change the type of the data shown in the tabs... Hope I could help :)

Owehbeh
  • 579
  • 1
  • 5
  • 16
  • Damn, that is one simple but working solution! What a priceless piece of code! Thanks a lot! – forumfresser Dec 31 '13 at 16:19
  • I still don't understand how this works inside a fragment that is attached to an Activity using a navigation Drawer. There are very few examples out there of this being used. Only one of my Fragments needs to hold tabs at the top of the window. – Jethro Feb 16 '16 at 01:44