Skip to content

Instantly share code, notes, and snippets.

@madhur
Forked from jerolimov/tabsample.xml
Created May 20, 2014 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madhur/7230c3dcb8572bec7dc1 to your computer and use it in GitHub Desktop.
Save madhur/7230c3dcb8572bec7dc1 to your computer and use it in GitHub Desktop.
<?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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment