Search code examples
androidandroid-fragmentsandroid-tabhost

FragmentTabHost shows content in tab


enter image description hereim trying to set up a FragmentTabHost with 3 tabs. The problem im having is the content of each tab is actually being displayed in the tab tittle as well. Why is this happening? The layout for tab1 has a textview that says "Tab1" this is being displayed in the tab and not the content area. also if i change the background it changes the background of the tabs not the content area.

xml:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.app.FragmentTabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

            <FrameLayout
                android:id="@+id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

MAIN Fragment:

public class NewFragment _Frag extends Fragment{
    private FragmentTabHost mTabHost;



    Intent intent;
  boolean created = false;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        //return super.onCreateView(inflater, container, savedInstanceState);
        if(container==null)
        {
            return null;

        }
          View v = inflater.inflate(R.layout.mylayout, container, false);

          mTabHost = (FragmentTabHost)v.findViewById(android.R.id.tabhost);
            mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);



            mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Tab1"),
                      myclass1.class, null);



            mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Tab2"),
                    myclass2.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Tab3"),
                    myclass3.class, null);




          return v;
    }

Solution

  • First, lets assume your XML name is my_xml.xml. So make following changes in the my_xml.xml first(Change the ID)

    <android.support.v4.app.FragmentTabHost
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/my_tabhost"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    

    and then following changes in the java file.

    mTabHost = (FragmentTabHost)v.findViewById(R.id.my_tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_xml);