tags:

views:

25

answers:

1

Hey,

I'm trying to set up a tab host as an element of a layout. All the examples I've seen show the TabHost by itself in the view. The examples work fine, but when trying to make it part of another layout it doesn't seem to work. I haven't found any documentation specifying one way or another.

Something like this works (Not all code shown for illustration purposes):

            <TabHost>
                    <LinearLayout>
                        <TabWidget/>
                        <FrameLayout>
                            <TextView
                                android:text="this is a tab" />
                            <TextView
                                android:text="this is another tab" />
                            <TextView
                                android:text="this is a third tab" />
                        </FrameLayout>
                    </LinearLayout>
            </TabHost>

However this doesn't work:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&gt;


Am I doing something wrong or is this just not possible ?

This is my onCreate method:

public class MyActivity extends Activity {

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

    }
}

Is there something that needs to be set there ?

When I say it doesn't work I mean that after running the application on the emulator I get a popup saying: "The application xxx (process com.example) has stopped unexpectedly. Please try again."

If I remove the tab code it works again. I'm developing for android 1.6 if that makes a difference.

A: 

It's working!!

Thanks to you guys saying it was supposed to work I kept searching for the problem comparing with the examples in which I have TabHost working by itself.

The issue was with my onCreate() method. I needed to get the tab host and add the tabs there, I (for some reason) thought it was enough just in the xml.

My onCreate ended up looking something like this: (for someone making the same dumb mistake as me)

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

    TabHost mTabHost = getTabHost();
    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Tab 1").setContent(R.id.textview1));
    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Tab 2").setContent(R.id.textview2));
    mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Tab 3").setContent(R.id.textview3));

    mTabHost.setCurrentTab(0);        

}
Lilitu88