title: Custom style for Android's TabWidget
body: <p>I am using tabs in my Android app, and I have run into this problem when running the app on HTC Sense phones:</p>
  
  <p><a href="https://stackoverflow.com/questions/3647821/android-highlighted-tab-of-tabwidget-not-readable-on-htc-sense">Android: Highlighted tab of TabWidget not readable on HTC Sense</a></p>
  
  <p>The solution suggested there (setting android:targetSdkVersion to 4) does not fix the problem for me, nor do I want to set the target sdk to 4.</p>
  
  <p>I have instead tried solving this problem my creating my own tab widget style, and modifying the text color. The problem is that there is no noticable difference when I use my own style; i.e. the style does not appear to be applied to the tabs.</p>
  
  <p>This is the code for the main activity, holding the tabs:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.tab);
  
      tabHost = getTabHost();
      tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1").setContent(new Intent(this, Tab1.class)));
      tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2").setContent(new Intent(this, Tab2.class)));
  
      tabHost.setCurrentTab(0);
  }
  </code></pre>
  
  <p>This is my tab.xml. Notice that I have specified MyTabStyle as style for TabWidget:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"&gt;
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="5dp"&gt;
          &lt;TabWidget
              style="@style/MyTabStyle"
              android:id="@android:id/tabs"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" /&gt;
          &lt;FrameLayout
              android:id="@android:id/tabcontent"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="5dp" /&gt;
      &lt;/LinearLayout&gt;
  &lt;/TabHost&gt;
  </code></pre>
  
  <p>And this is my definition of MyTabStyle, which I have defined in res/values/styles.xml:</p>
  
  <pre><code>&lt;style name="MyTabStyle" parent="@android:style/TextAppearance.Widget.TabWidget"&gt;
      &lt;item name="android:textColor"&gt;#5DFC0A&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>Why do no changes in MyTabStyle show up in my app? Any other solutions to solving the invisible text on selected tabs in HTC Sense?</p>
  
  <p><strong>UPDATE 2011-06-02</strong></p>
  
  <p>I managed to solve this in a sort of hacky way, by using the knowledge that the text on the tabs are actually TextViews. Add the following method to your activity:</p>
  
  <pre><code>private void setTabColor(TabHost tabHost) {
      try {
          for (int i=0; i &lt; tabHost.getTabWidget().getChildCount();i++) {
              TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
              if (tv != null) {
                  tv.setTextColor(Color.parseColor("#ffffff"));
              }
              TextView tv2 = (TextView) tabHost.getCurrentTabView().findViewById(android.R.id.title); // Selected Tab
              if (tv2 != null) {
                  tv2.setTextColor(Color.parseColor("#000000"));
              }
          }
      } catch (ClassCastException e) {
          // A precaution, in case Google changes from a TextView on the tabs.
      }
  }
  </code></pre>
  
  <p>Add the following in your onCreate() method in your activity:</p>
  
  <pre><code>// Only apply the fix if this is a HTC device.
  if (android.os.Build.MANUFACTURER.toLowerCase().contains("htc")) {
      // Set up the color initially
      setTabColor(tabHost);
  
      // Add a tab change listener, which calls a method that sets the text color.
      tabHost.setOnTabChangedListener(new OnTabChangeListener() {
          public void onTabChanged(String tabId) {
              InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);
              setTabColor(tabHost);
          }
      });
  }
  </code></pre>