Same as my previous post on TabBar as mentioned below, this article is also on TabBar and it is for changing Background color of Tab bar.
Article 1: Android – simple Tab bar example
Article 2: Android – iPhone like Tab bar in Android
Problem: How to change Tab Bar background color in android?
Solution:
Step 1: Write the below code in onCreate() method to define initial background color for the selected and unselected tab.
1 2 3 4 5 6 | for ( int i= 0 ;i<tabHost.getTabWidget().getChildCount();i++) { tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor( "#8A4117" )); } tabHost.getTabWidget().setCurrentTab( 1 ); tabHost.getTabWidget().getChildAt( 1 ).setBackgroundColor(Color.parseColor( "#C35817" )); |
Step 2: implement the OnTabChangeListener to current activity and then override the onTabChanged() method. In that method, write the below code to define color for selected tab and unselected tab.
01 02 03 04 05 06 07 08 09 10 | @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub for ( int i= 0 ;i<tabHost.getTabWidget().getChildCount();i++) { tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor( "#8A4117" )); } tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor( "#C35817" )); } |
For Example:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package com.technotalkative.tabbarexample; import android.app.TabActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; public class TabBarExample extends TabActivity implements OnTabChangeListener{ /** Called when the activity is first created. */ TabHost tabHost; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); tabHost = getTabHost(); tabHost.setOnTabChangedListener( this ); TabHost.TabSpec spec; Intent intent; // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass( this , FirstActivity. class ); spec = tabHost.newTabSpec( "First" ).setIndicator( "First" ) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass( this , SecondActivity. class ); spec = tabHost.newTabSpec( "Second" ).setIndicator( "Second" ) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass( this , ThirdActivity. class ); spec = tabHost.newTabSpec( "Third" ).setIndicator( "Third" ) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass( this , FourthActivity. class ); spec = tabHost.newTabSpec( "Fourth" ).setIndicator( "Fourth" ) .setContent(intent); tabHost.addTab(spec); for ( int i= 0 ;i<tabHost.getTabWidget().getChildCount();i++) { tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor( "#8A4117" )); } tabHost.getTabWidget().setCurrentTab( 1 ); tabHost.getTabWidget().getChildAt( 1 ).setBackgroundColor(Color.parseColor( "#C35817" )); } @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub for ( int i= 0 ;i<tabHost.getTabWidget().getChildCount();i++) { tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor( "#8A4117" )); } tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor( "#C35817" )); } } |