Search code examples
androidandroid-fragmentsfragment-tab-host

Content within fragment going out of area when Tabhost implemented within Fragment Android?


I have implemented FragmentTabHost with three tabs. They are working fine when changing tabs. However contents within each fragment of tab is going out of screen area. It looks like as shown following:

Content within fragment going on tab rather than white area available

Following is my code:

My Parent Fragment Java code:

public class TestFragment extends Fragment implements OnTabChangeListener {
    private FragmentTabHost mTabHost;
    private FrameLayout realtabcontent;

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

        View rootView = inflater.inflate(R.layout.test_fragment, container,
                false);
        realtabcontent = (FrameLayout) rootView
                .findViewById(R.id.realtabcontent);
        mTabHost = (FragmentTabHost) rootView
                .findViewById(android.R.id.tabhost);
        // mTabHost.setup(getActivity(), getChildFragmentManager(),
        // R.id.realtabcontent);
        mTabHost.setup(getActivity(), getChildFragmentManager());
        mTabHost.setOnTabChangedListener(this);
        mTabHost.addTab(
                mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
                FragmentB.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"),
                FragmentC.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"),
                FragmentD.class, null);

        return rootView;
    }

Parent Fragment XML:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent" >

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.v4.app.FragmentTabHost>

Child fragments contain normal textview right now.


Solution

  • You really doesn't need to take FrameLayout in the XML FragmentTabHost manage this. You just need to add your XML id in the setup Method like below.

    mTabHost.setup(getActivity(), getChildFragmentManager(),R.layout.test_fragment);