Question

I've created a TabHost and assigned 4 activity intents with tabs and these seems to work just fine. My only problem is that the activity content is not showing within the framelayout #tabcontent in my tabhost view.

I've followed the official reference and scanned the internet for a solution but I can't see what the problem is.

The Log.v("Activity", "Reports") is logged in ant, so it does execute the activity. Therefore I'm guessing its the setContentView() in my ReportsActivity that is causing the problem. But I'm new to this so I can't really tell. (There are no generated errors)

This is my tabhost

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabhost"
    android:background="#FFFFFF">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5sp"
            android:layout_weight="1" />

    </LinearLayout>

</TabHost>

This is how I add tabs in my TabActivity

// Glob
Intent intent;
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Resources res = getResources();

// Reports tab
intent = new Intent().setClass(this, ReportsActivity.class);
spec = tabHost.newTabSpec("reports")
        .setIndicator(
                res.getText(R.string.reports),
                res.getDrawable(R.drawable.reports))
        .setContent(intent);
tabHost.addTab(spec);

And this is my content activity (R.layout.reports = linearlayout with a textview)

public class ReportsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reports);

        Log.v("Activity", "Reports");
    }
}
Was it helpful?

Solution

Try implementing your TabSpec like this:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

spec = tabHost.newTabSpec("reports")
              .setIndicator(
                  res.getText(R.string.reports),
                  res.getDrawable(R.drawable.reports))
              .setContent(intent);

OTHER TIPS

This is because you have chosen a LinearLayout which defaults to Horizontal layout. If you set android:orientation="vertical" inside the LinearLayout tag that should fix your problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top