I'm trying to create a simple 2 tab interface for an Android application, and this is proving to be a bigger pain than I would have thought. Testing my application in Android 1.5 (SDK 3), both tabs appear white, as if they are both focused. This problem does not occur in more recent SDK versions.
Here is the code I am using:
TabbedInterface.java
public class TabbedInterface extends TabActivity {
TabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabbed_interface);
setTitle(R.string.app_name);
mTabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec;
setupTab1();
setupTab2();
mTabHost.setCurrentTab(0);
}
private void setupTab1() {
Intent intent = new Intent();
intent.setClass(this, Activity1.class);
mTabHost.addTab(mTabHost.newTabSpec("tab1")
.setIndicator("Tab 1",
getResources().getDrawable(R.drawable.ic_tab_icon))
.setContent(intent));
}
private void setupTab2() {
Intent intent = new Intent();
intent.setClass(this, Activity2.class);
mTabHost.addTab(mTabHost.newTabSpec("tab2")
.setIndicator("Tab 2",
getResources().getDrawable(R.drawable.ic_tab_icon))
.setContent(intent));
}
}
tabbed_interface.xml
<?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"
android:padding="0dip">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
It's become clear to me that visual elements are styled much differently across different versions of Android and for that reason, I would like to apply a custom layout/style to the tabs to enforce a uniform appearance across different versions of Android.
I thought I found an answer to this problem - a related question here on StackOverflow had an answer recommending the user style their tabs in the Java code by calling setIndicator() on a TabSpec. However, you cannot do this in Android 1.5 - Eclipse will just tell me there's an error and it won't let me compile. My other solution was to try and copy Android's tab styling for their native Music and Contacts applications (which utilize tabs that contrast in style/UI guidelines from the default TabWidget appearance), which are darker and flatter. The source for the buttonbar.xml file from the Music app shows this custom layout in the form of styled TextViews that are nested within a TabWidget. However, the Java code for the rest of the application is so dense, I can't figure out how to tie tab switching functionality to the TextViews.
So that leaves me at a loss for how I support a tabbed interface for Android 1.5. Ideally, I'd like to define a custom layout for the tabs that is compatible with all forms of Android, but at this point, I'd just like to get the appearance right any way that I can.