1

I wrote some app that contain TabHost. Each tab contain a LinearLayout - and i want to add some different exist Fragment that i created to each of the tab

How to do it ?

private void setNewTab(Context context, TabHost tabHost, String tag, String title, int contentID ){
    TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);

    tabSpec.setIndicator(title);


    Fragment newFragment = new ActionFragment ();


    tabSpec.setIndicator(newFragment.getView());


    tabSpec.setContent(contentID);
    tabHost.addTab(tabSpec);                     // after this line i get an exception


}




<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TabWidget>

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

        <LinearLayout
            android:id="@+id/tab1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 1 Content" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_red_dark">
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 2 Content" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_green_light">
            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 3 Content" />
        </LinearLayout>


    </FrameLayout>

</LinearLayout>

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Yanshof
  • 9,659
  • 21
  • 95
  • 195

2 Answers2

4

Use ViewPager with FragmentStatePagerAdapter if you many tabs. If you have only 2 to 3 tabs use PagerAdapter.

Check out the below link, it explained very well how any why to use ViewPager with TabLayout.

https://guides.codepath.com/android/google-play-style-tabs-using-tablayout

TabLayout with ViewPager goes well in your scenario.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Ritt
  • 3,181
  • 3
  • 22
  • 51
1

A quick search of "Android Fragment TabHost" into the documentation results in the FragmentTabHost class. Are you using it?

    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

  mTabHost.addTab(mTabHost.newTabSpec("action").setIndicator("action"), ActionFragment.class, null);

I still think TabLayout would be a easier option.

If you want tabs at the bottom, there's even options for that.

Which view should be used for new Material Design Bottom Navigation?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245