Difference between revisions of "Change Tab Color"

From John Freier
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 9: Line 9:
  
 
To set different color for current selected tab. use
 
To set different color for current selected tab. use
view source
+
 
print?
+
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#4E4E9C"));
1 tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#4E4E9C"));
+
  
 
Here 0 position is the first tab.
 
Here 0 position is the first tab.
  
 
When you change the tab selection you need to change your selected color to that current tab & replace the default color to other tab. For that use OnTabChangeListener(). These method will be called when you change the tab selection.
 
When you change the tab selection you need to change your selected color to that current tab & replace the default color to other tab. For that use OnTabChangeListener(). These method will be called when you change the tab selection.
view source
+
 
print?
+
tabHost.setOnTabChangedListener(new OnTabChangeListener()
01 @Override
+
{
02 public void onTabChanged(String tabId) {
+
      public void onTabChanged(String tabId)
03 // TODO Auto-generated method stub
+
      {
04 for(int i=0;i
+
          for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
05 {
+
          {
06 tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
+
07 }
+
                // Change the background color of all the tabs.
08
+
                tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#000000"));
09 tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#4E4E9C"));
+
          }
10 }
+
          tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#4E4E9C"));
 +
      }
 +
});
 +
 
 +
When using Color.parseColor, use
 +
import android.graphics.Color;

Latest revision as of 00:05, 13 February 2011

This is sample code to change the tab background color.

Use the below line of code in onCreate() to display in first time.

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
     tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
}

To set different color for current selected tab. use

tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#4E4E9C"));

Here 0 position is the first tab.

When you change the tab selection you need to change your selected color to that current tab & replace the default color to other tab. For that use OnTabChangeListener(). These method will be called when you change the tab selection.

tabHost.setOnTabChangedListener(new OnTabChangeListener()
{
     public void onTabChanged(String tabId)
     {
          for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
          {

               // Change the background color of all the tabs.
               tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#000000"));
          }
          tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#4E4E9C"));
     }
});

When using Color.parseColor, use

import android.graphics.Color;