Question

This is my first tabbed application for Android. I walked through the "HelloTabWidget" app on androids website, but I can't figure out how to add content to the tabs. The FrameLayout stacks stuff on top of each other to the top left (from what I've read). I added a couple of textviews and an imageView, but it only displays the last item added. Is there a way to use Linear Layout instead of Frame Layout? If not, how can you place multiple views in the tab? The only thing I have done different from the example is I added a 4th tab. In one of the tab Activities I inserted the following code to try to get multiple items to display:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    

        TextView textview = new TextView(this);
        textview.setText("This is the About tab");
        setContentView(textview);

        TextView textview2 = new TextView(this);
        textview2.setText("About Test");
        setContentView(textview2);

        ImageView imgView = new ImageView(this);
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.header));
        setContentView(imgView);
    }

Here is the link to the example I followed: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

Was it helpful?

Solution

What is your layout xml file? I use this and I can stack several textview in every tab. TabActivity:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView android:layout_width="fill_parent" 
        android:layout_height="wrap_content" >
        <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <TabWidget android:id="@android:id/tabs"
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" />
            <FrameLayout android:id="@android:id/tabcontent"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" />
        </LinearLayout>
    </ScrollView>
</TabHost>

Activity inside tab:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView android:id="@+id/textOne" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/textTwo" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
<LinearLayout>

I'm a newbie myself, so there might be a better way to do it. This works for me, though.

OTHER TIPS

You didn't read the example carefully. Take a look at the point number 6; you will see something like:

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

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ArtistsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, AlbumsActivity.class);
    spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                      res.getDrawable(R.drawable.ic_tab_albums))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SongsActivity.class);
    spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                      res.getDrawable(R.drawable.ic_tab_songs))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(2);
}

That's what you put in the onCreate for the TabActivity. As you can see it has 3 activities. What you are doing is using JUST ONE activity and setting the content view 3 times, which is obviously wrong.

So... how to make it work? First, read again the tutorial. Second, create one activity for each tab you want to show. And use the model above to add those activities to your TabHost.

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