Search code examples
javaandroidandroid-fragmentsandroid-tabhost

Android weighted fragment not showing


Basically what I'm trying to do is have half the screen be a tab layout and half an image (or really anything else). It works just fine until I start adding weight.

As is, nothing is displayed. What would cause this problem?

<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"
    android:orientation="horizontal"
    android:gravity="left"
    android:weightSum="2"
    android:background="#00025a">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">
        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs">
        </TabWidget>

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

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

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/test_room_1"/>

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

Without the weight component the tab related stuff displays, but I'm assuming it pushes the image out as it isn't shown.

Any help would be appreciated. :)

UPDATE:

Here's a screenshot as requested: Weighted

And the calling activity:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.ImageView;

public class sandbox_mode extends FragmentActivity {
    ImageView level_background;

    private FragmentTabHost mTabHost;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sandbox_mode_play);

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

        mTabHost.addTab(mTabHost.newTabSpec("Enemy Tab").setIndicator("Enemies"),
                enemy_tab.class, null);
    }
}

Solution

  • If you look at the documentation on FragmentTabHost link, you'll see that it's not inherited from LinearLayout, so basically layout_weight is not supported by this container. I would suggest adding additional top LinearLayout for this to work correctly.

    <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"
    android:orientation="horizontal"
    android:gravity="left"
    android:weightSum="2"
    android:background="#00025a">
    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="horizontal"
       android:weightSum="2" />
    
        <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">
        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs">
        </TabWidget>
    
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@android:id/tabcontent">
        </FrameLayout>
    
        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </FrameLayout>
        </LinearLayout>
    
        <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/test_room_1"/>
    </LinearLayout>
    
    </android.support.v4.app.FragmentTabHost>