Search code examples
androidandroid-fragmentsfragment-tab-host

android - fragmenttabhost not showing inside my fragment


My classes

FragmentTabs - the fragment that will hold my FragmentTabHost

Its onCreateView method

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


    fragmentTabHost = new FragmentTabHost(getActivity());
    fragmentTabHost.setup(getActivity(), getFragmentManager());

    fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab1").setIndicator("Tab1"), Fragment1.class, null);
    fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab2").setIndicator("Tab2"), Fragment2.class, null);
    return fragmentTabHost;

}

The activity class XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.studio3s.myapplication.TestActivity">


<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.studio3s.myapplication.FragmentTabs"
    android:id="@+id/fragment"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

I see no tabs on my activity..where can be the problem?

Fragment1 and 2 classes are just empty fragments with just one textview..


Solution

  • use childFragmentManager instead FragmentManager i.e.

    fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.content);

    Here conent is where you want to shown current fragment. now your container will contains tabs which you add in fragmentTabHost