I created an extension of Tab Layout to allow add many elements in TabHost.
public class TabScrollHost {
private TabHost tabHost;
private static final int TAB_MAX = 4;
private List<TabHost.TabSpec> listTab;
private int currentPage = 0;
private ImageView tab_next;
private ImageView tab_previous;
public TabScrollHost(TabHost tabHost,ImageView tab_next,ImageView tab_previous) {
this.listTab = new ArrayList<TabHost.TabSpec>();
this.currentPage = 0;
this.tabHost = tabHost;
this.tab_previous = tab_previous;
this.tab_next = tab_next;
this.tab_previous.setVisibility(View.INVISIBLE);
this.tab_next.setVisibility(View.INVISIBLE);
this.tab_next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClickNext();
}
});
this.tab_previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClickPrevious();
}
});
}
public void onClickNext() {
currentPage++;
this.tabHost.setCurrentTab(0);
this.tabHost.clearAllTabs();
int start = currentPage * TAB_MAX;
int end = start + TAB_MAX;
if(end >= listTab.size()){
end = listTab.size();
this.tab_next.setVisibility(View.INVISIBLE);
}
Log.v(“TabScrollHost”, “start:”+start+” end:”+end);
Log.v(“TabScrollHost”, “listTab:”+listTab);
List<TabHost.TabSpec> subList = listTab.subList(start, end);
for (TabHost.TabSpec spec : subList) {
this.tabHost.addTab(spec);
}
//this.tabHost.setCurrentTab(0);
this.tab_previous.setVisibility(View.VISIBLE);
}
public void onClickPrevious() {
currentPage–;
this.tabHost.setCurrentTab(0);
this.tabHost.clearAllTabs();
int start = currentPage * TAB_MAX;
int end = start + TAB_MAX;
Log.v(“TabScrollHost”, “start:”+start+” end:”+end);
Log.v(“TabScrollHost”, “listTab:”+listTab);
List<TabHost.TabSpec> subList = listTab.subList(start, end);
for (TabHost.TabSpec spec : subList) {
this.tabHost.addTab(spec);
}
//this.tabHost.setCurrentTab(0);
if(currentPage <=0)
this.tab_previous.setVisibility(View.INVISIBLE);
this.tab_next.setVisibility(View.VISIBLE);
}
public void addTab(TabHost.TabSpec tabSpec) {
Log.v(“TabScrollHost”, “addTab”);
listTab.add(tabSpec);
if (listTab.size() <= TAB_MAX) {
this.tabHost.addTab(tabSpec);
}else{
this.tab_next.setVisibility(View.VISIBLE);
}
}
public void setCurrentTab(int index){
this.tabHost.setCurrentTab(index);
}
}
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost1 = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
ImageView tab_next = (ImageView) findViewById(R.id.tab_next);
ImageView tab_previous = (ImageView) findViewById(R.id.tab_previous);
TabScrollHost tabHost = new TabScrollHost(tabHost1,tab_next,tab_previous);
intent = new Intent().setClass(this,Tab1Activity.class);
spec = tabHost1.newTabSpec(“tab1”).setIndicator(“Tab1”,
res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab1Activity.class);
spec = tabHost1.newTabSpec(“tab2”).setIndicator(“Tab2”,
res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab1Activity.class);
spec = tabHost1.newTabSpec(“tab3”).setIndicator(“Tab3”,
res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab1Activity.class);
spec = tabHost1.newTabSpec(“tab4”).setIndicator(“Tab4”,
res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab1Activity.class);
spec = tabHost1.newTabSpec(“tab5”).setIndicator(“Tab5”,
res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab1Activity.class);
spec = tabHost1.newTabSpec(“tab6”).setIndicator(“Tab6”,
res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab1Activity.class);
spec = tabHost1.newTabSpec(“tab7”).setIndicator(“Tab7”,
res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab1Activity.class);
spec = tabHost1.newTabSpec(“tab8”).setIndicator(“Tab8”,
res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab1Activity.class);
spec = tabHost1.newTabSpec(“tab9”).setIndicator(“Tab9”,
res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab1Activity.class);
spec = tabHost1.newTabSpec(“tab10”).setIndicator(“Tab10”,
res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
public class Tab1Activity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText(“This is the Artists tab”);
setContentView(textview);
}
}