Question

I'm having troubles trying to style my tabs in android.

I want to make them look exactly the same as whats in the open source android contacts list (see https://android.googlesource.com/platform/packages/apps/Contacts ).

Problem is that when they display on the screen it looks a bit different to the contacts app.

my tabs

When it should look like this:

enter image description here

Notice how the background colors are a little bit different and the text colors are different.

Not sure why this is the case as its basically the same code and icons.

My tab layout code is the following:

<?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="0dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="0dp" />
    </LinearLayout>
</TabHost>

Which doesn't contain anything special there.. and the TabActivity is as follows:

public class TabbedActivity extends TabActivity implements
        TabHost.OnTabChangeListener {

    private TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Intent intent = getIntent();

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.tab);

        tabHost = getTabHost();
        tabHost.setOnTabChangedListener(this);

        setupLatestTab();
        setupSavedTab();

        tabHost.setCurrentTab(0);
    }

    private void setupLatestTab() {

        Intent intent = new Intent().setClass(this, ResultsActivity.class);

        tabHost.addTab(tabHost
                .newTabSpec("latest")
                .setIndicator("Latest",
                        getResources().getDrawable(R.drawable.ic_tab_recent))
                .setContent(intent));
    }

    private void setupSavedTab() {

        Intent intent = new Intent().setClass(this, ResultsActivity.class);

        tabHost.addTab(tabHost
                .newTabSpec("saved")
                .setIndicator("Saved",
                        getResources().getDrawable(R.drawable.ic_tab_starred))
                .setContent(intent));
    }

    @Override
    public void onTabChanged(String tabId) {
        // Because we're using Activities as our tab children, we trigger
        // onWindowFocusChanged() to let them know when they're active. This may
        // seem to duplicate the purpose of onResume(), but it's needed because
        // onResume() can't reliably check if a keyguard is active.
        Activity activity = getLocalActivityManager().getActivity(tabId);
        if (activity != null) {
            activity.onWindowFocusChanged(true);
        }
    }

}

I am using the same images from the drawable folders too.

I know i can set the background of tabs manually by doing something like this in the tabactivity

tabHost.getTabWidget().getChildAt(index).setBackgroundColor(Color.parseColor("#ff202020"));

But the contacts app isn't doing this sort of thing anywhere (most of the top tab code is in DialtactsActivity), so just want to do what the open source app is doing when displaying tabs - i'm not sure how and why the contacts application tabs look much better when im basically using the same code and resources.

I guess im just missing something trivial??

Was it helpful?

Solution

This turned out to be a problem with my minimum android version not being specified ..

Added:

<uses-sdk android:minSdkVersion="8" />

to the androidmanifest and it worked fine. I guess it was reverting to the old tabs look and feel in earlier versions of android.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top