Android tab customisation

  • Replies:0
Ashfaq
  • Forum posts: 17

Nov 28, 2014, 10:47:52 AM via Website

Hi friends,
Here is my code for android tab.
I want to customise the colour of tab while clicking.The style i written in tab.xml is not working.
Im bit confused with other tutorials,Please help me to solve it.

JAVA

import android.app.ActionBar.LayoutParams;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

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

        TabHost tabHost = getTabHost();

        // Tab for Photos
        TabSpec overviewspec = tabHost.newTabSpec("Overview");
        overviewspec.setIndicator("Overview");
        Intent overviewspecIntent = new Intent(this, TulipOverviewActivity.class);
        overviewspec.setContent(overviewspecIntent);

        // Tab for Songs
        TabSpec locationmapspec = tabHost.newTabSpec("Location Map");
        // setting Title and Icon for the Tab
        locationmapspec.setIndicator("Location Map");
        Intent locationmapspecIntent = new Intent(this, TulipOverviewActivity.class);
        locationmapspec.setContent(locationmapspecIntent);

        // Tab for Videos
        TabSpec amenitiesspec = tabHost.newTabSpec("Amenities");
        amenitiesspec.setIndicator("Amenities");
        Intent amenitiesspecIntent = new Intent(this, TulipOverviewActivity.class);
        amenitiesspec.setContent(amenitiesspecIntent);

     // Tab for Videos
        TabSpec specificationsspec = tabHost.newTabSpec("Specifications");
        specificationsspec.setIndicator("Specifications");
        Intent specificationsspecIntent = new Intent(this, TulipOverviewActivity.class);
        specificationsspec.setContent(specificationsspecIntent);


     // Tab for Videos
        TabSpec plansspec = tabHost.newTabSpec("Plans");
        plansspec.setIndicator("Plans");

        Intent plansspecIntent = new Intent(this, TulipOverviewActivity.class);
        plansspec.setContent(plansspecIntent);




        // Adding all TabSpec to TabHost
        tabHost.addTab(overviewspec); // Adding photos tab
        tabHost.addTab(locationmapspec); // Adding songs tab
        tabHost.addTab(amenitiesspec); // Adding videos tab
        tabHost.addTab(specificationsspec); // Adding videos tab
        tabHost.addTab(plansspec); // Adding videos tab

        for (int tabIndex = 0 ; tabIndex < tabHost.getTabWidget().getTabCount() ; tabIndex ++) {
            View tab = tabHost.getTabWidget().getChildTabViewAt(tabIndex);
            TextView t = (TextView)tab.findViewById(android.R.id.title);
            t.setTextColor(getResources().getColor(R.color.tab_not_select));
            t.setTextSize(12);

        }


        tabHost.setClickable(true);
        tabHost.setBackgroundColor(Color.WHITE);
       // tabHost.setPadding(10, 10, 10, 10);




        tabHost.getTabWidget().getChildAt(0).setBackgroundColor(R.drawable.tab);
        tabHost.getTabWidget().getChildAt(1).setBackgroundColor(R.drawable.tab);
        tabHost.getTabWidget().getChildAt(2).setBackgroundColor(R.drawable.tab);
        tabHost.getTabWidget().getChildAt(3).setBackgroundColor(R.drawable.tab);
        tabHost.getTabWidget().getChildAt(4).setBackgroundColor(R.drawable.tab);

       // tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#ffcd14"));
    }
}

tab.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@color/transparent"/>
  <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@color/tab_bottom"/>
  <item android:state_selected="false" android:state_pressed="true" android:drawable="@color/tab_highlight"/>
  <item android:state_selected="true" android:state_pressed="true" android:drawable="@color/tab_bottom"/>
</selector>

main.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">
        <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"/>
    </LinearLayout>
</TabHost>

Reply