Skip to content

Instantly share code, notes, and snippets.

@christoph-jerolimov
Created October 9, 2010 10:15
Show Gist options
  • Save christoph-jerolimov/618086 to your computer and use it in GitHub Desktop.
Save christoph-jerolimov/618086 to your computer and use it in GitHub Desktop.
Android TabHost TabWidget TabContent widget sample
<?xml version="1.0" encoding="utf-8"?>
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:tag="tab0"
android:text="Tab 1"
android:background="@android:drawable/btn_star_big_on"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<TextView
android:tag="tab1"
android:text="Tab 2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<TextView
android:tag="tab2"
android:text="Tab 3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Hallo1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:text="Hallo2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:text="Hallo3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
package com.buenocode.sensortests;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
public class SensorActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabsample);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
final TabWidget tabWidget = tabHost.getTabWidget();
final FrameLayout tabContent = tabHost.getTabContentView();
// Get the original tab textviews and remove them from the viewgroup.
TextView[] originalTextViews = new TextView[tabWidget.getTabCount()];
for (int index = 0; index < tabWidget.getTabCount(); index++) {
originalTextViews[index] = (TextView) tabWidget.getChildTabViewAt(index);
}
tabWidget.removeAllViews();
// Ensure that all tab content childs are not visible at startup.
for (int index = 0; index < tabContent.getChildCount(); index++) {
tabContent.getChildAt(index).setVisibility(View.GONE);
}
// Create the tabspec based on the textview childs in the xml file.
// Or create simple tabspec instances in any other way...
for (int index = 0; index < originalTextViews.length; index++) {
final TextView tabWidgetTextView = originalTextViews[index];
final View tabContentView = tabContent.getChildAt(index);
TabSpec tabSpec = tabHost.newTabSpec((String) tabWidgetTextView.getTag());
tabSpec.setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
return tabContentView;
}
});
if (tabWidgetTextView.getBackground() == null) {
tabSpec.setIndicator(tabWidgetTextView.getText());
} else {
tabSpec.setIndicator(tabWidgetTextView.getText(), tabWidgetTextView.getBackground());
}
tabHost.addTab(tabSpec);
}
// tabHost.setCurrentTab(0);
}
}
@mahdimortazavi
Copy link

How can you swipe between the tabs?
With the touch screen to the right and left

@xucaen
Copy link

xucaen commented Oct 3, 2016

Why couldn't I use separate layouts to fill in the tabs?

@JoaquinGonzalez
Copy link

Rendering Problem

Exception raised during rendering: Could not create tab content because could not find view with id -1

@Fyesinho
Copy link

set the TextView id's

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment