Search code examples
androidandroid-fragmentsfragment-tab-host

Content of FragmentTabHost Tabs is not appearing


Here is my activity_main.xml layout which is holding two tabs. The selected tab content is not getting displayed at all. Where I'm going wrong? The tab content is getting displayed if I swap the places of FragmentTabHost and Framelayout in below layout i.e Making the tabs appear at bottom. But I want my tabs to be at top of the screen. Please help me.

<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
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

 <FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" />

</LinearLayout>

MainActivity

 public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

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

    LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View v1 = inflator.inflate(R.layout.tab_bg, null);
    TextView tv1 = (TextView)v1.findViewById(R.id.tabTitleTextView);
    tv1.setText("Tab1");

    View v2 = inflator.inflate(R.layout.tab_bg, null);
    TextView tv2 = (TextView)v2.findViewById(R.id.tabTitleTextView);
    tv2.setText("Tab2");

    mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator(v1), FragmentTab.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator(v2), FragmentTab.class, null);
    mTabHost.setCurrentTab(0);       
}

}

FragmentTab :

 public class FragmentTab extends Fragment {
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)    {
    View v = inflater.inflate(R.layout.fragment_layout, null);
    TextView tv = (TextView) v.findViewById(R.id.text);
    tv.setText(this.getTag() + " Content");
    return v;
}
 }

Solution

  • try this:

    <android.support.v4.app.FragmentTabHost
        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" >
    
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />
    
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    
        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
    

    and change

    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
    

    to

     mTabHost.setup(this, getSupportFragmentManager(),R.id.realtabcontent);