0

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");
    }
}
Daniel
  • 3,726
  • 4
  • 26
  • 49

2 Answers2

0

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

fuzzyrob
  • 31
  • 3
0

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);
Dyonisos
  • 3,541
  • 2
  • 22
  • 25
  • The TabSpec has no method addFlags so I tried to use the flag on my intent instead, but still the tabcontent does not show my ReportsActivity linearlayout. **Edit:** Should my ReportsActivity extend any other activity than the default Activity superclass? – Daniel Jan 02 '12 at 11:58
  • If I remove the TabWidget in my xml the FrameLayout suddenly shows the activity content. I try to use margins to see if the TabWidget is overlapping the FrameLayout but that is not the case (I think). Should the TabWidget have anything to do with this? – Daniel Jan 02 '12 at 13:25
  • I didnt see, that you initialized the intent outside of the tabspec. The addFlag belongs to the intent. See my corrected answer. – Dyonisos Jan 02 '12 at 15:00
  • Yeah, I got that - the problem persists. If i remove the TabWidget element from my XML the framelayout will show the activity content, but if I implement the tabwidget element again the activity content will not show. So, I'm guessing there is a problem with my setContentView or tabhost? – Daniel Jan 02 '12 at 18:12
  • Turns out that the android:orientation="vertical" is required for the linearlayout in the tabhost, everything is working now. – Daniel Jan 03 '12 at 07:47