Search code examples
androidandroid-tabhostandroid-tabsandroid-tabactivity

how style tabactivity in android


Hi im trying to style my tabactivity im using this:

public class TabContainer extends TabActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_container);

    //TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
    TabHost tabHost = getTabHost();

    // Tab for Radio
    TabSpec radioTabSpec = tabHost.newTabSpec("Radio Online");
    // setting Title and Icon for the Tab
    radioTabSpec.setIndicator("Radio Online", getResources().getDrawable(R.drawable.menu_radio));
    Intent radioIntent = new Intent(this, MainActivity.class);
    radioTabSpec.setContent(radioIntent);

    // Tab for Facebook
    TabSpec facebookTabSpec = tabHost.newTabSpec("Facebook");
    facebookTabSpec.setIndicator("Facebook", getResources().getDrawable(R.drawable.menu_facebook));
    Intent facebookIntent = new Intent(this, FacebookActivity.class);
    facebookTabSpec.setContent(facebookIntent);

    // Tab for Twitter
    TabSpec twitterTabSpec = tabHost.newTabSpec("Twitter");
    twitterTabSpec.setIndicator("Twitter", getResources().getDrawable(R.drawable.menu_twitter));
    Intent twitterIntent = new Intent(this, TwitterActivity.class);
    twitterTabSpec.setContent(twitterIntent);

    // Tab for About
    TabSpec aboutTabSpec = tabHost.newTabSpec("Acerca");
    aboutTabSpec.setIndicator("Acerca", getResources().getDrawable(R.drawable.menu_about));
    Intent aboutIntent = new Intent(this, AboutActivity.class);
    aboutTabSpec.setContent(aboutIntent);
    // Adding all TabSpec to TabHost

    tabHost.addTab(radioTabSpec); // Adding photos tab
    tabHost.addTab(facebookTabSpec); // Adding songs tab
    tabHost.addTab(twitterTabSpec); // Adding videos tab
    tabHost.addTab(aboutTabSpec); // Adding videos tab
}

}

here is my 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="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
</TabHost>

tabs style example:

enter image description here

i know tabactivity is old but i cant find any other way to make it work for android 2.3 and later now my tabs are showing totally black, i would like to style it to make it more nice.

thank you very much.


Solution

  • Why cant you use fragments.To support in all versions, use Action bar Sherlock library

    http://wptrafficanalyzer.in/blog/adding-navigation-tabs-containing-listview-to-action-bar-in-pre-honeycomb-versions-using-sherlock-library/