1

I want to create a tab dynamically in Android and under each tab I have one listview. So I want the content of the listview to also change dynamically. How can I do this ?

2 Answers 2

7

I saw a code on another forum and cleaned it up a little. To do what you need you'll have to change the stuff inside of ts1.setContent and ts2.setContent

Main_screen.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Main_screen extends Activity{

    private ListView ls1;
    private ListView ls2;   
    private TabHost myTabHost;

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

        myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost); 
        myTabHost.setup();

        ls1 = new ListView(Main_screen.this);       
        TabSpec ts1 = myTabHost.newTabSpec("TAB_TAG_1");
        ts1.setIndicator("Tab1");
        ts1.setContent(new TabHost.TabContentFactory(){
            public View createTabContent(String tag)
            {                                   
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item1","item2","item3"});
                ls1.setAdapter(adapter);    
                return ls1;
            }       
        });
        myTabHost.addTab(ts1);

        ls2 = new ListView(Main_screen.this);
        TabSpec ts2 = myTabHost.newTabSpec("TAB_TAG_2");
        ts2.setIndicator("Tab2");
        ts2.setContent(new TabHost.TabContentFactory(){
            public View createTabContent(String tag)
            {                                   
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item4","item5","item6"});
                ls2.setAdapter(adapter);    
                return ls2;
            }       
        });     
        myTabHost.addTab(ts2);
    }       
}

main.xml

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

     <TabWidget 
          android:id="@android:id/tabs" 
          android:layout_width="fill_parent" 
          android:layout_height="64dip" />

     <FrameLayout 
          android:id="@android:id/tabcontent" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"
          android:paddingTop="75dip">  

          <ListView
              android:id = "@+id/danhsach"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" />

     </FrameLayout> 
</TabHost>
0
for(int i=0; i<5; i++ )
{
    final TabSpec x=tabHost2.newTabSpec("x");
    View row = inflater.inflate(R.layout.indicator1,null);
    final TextView indicator1 =(TextView) row.findViewById(R.id.textView_indicator1);
    indicator1.setText(indicator_list[i]);
    // indicator1.setShadowLayer(1, 0, 1, 0xFF013201);

    x.setIndicator(row);

    x.setContent(new TabContentFactory() {
        public View createTabContent(String arg) {
        return gallery2;
        }
    });

    tabHost2.addTab(x);  
}

you can do like this.

1
  • 4
    an explanation of this answer, and why it might be an answer, would be useful for those who find this later. Sep 28, 2012 at 7:43

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.