title: Android Tab Layout tutorial?
body: <p>I want to be able to assign different images to my tabs in the TabLayout control depending on whether the item is selected or not. I followed the tutorial on the Android site but they made the example with just one image and for it it works. But it doesn't work for the rest of the tabs. How can I make it work? This is my code:</p>
  
  <p>The main activity:</p>
  
  <pre><code>public class Main extends TabActivity {
      private Resources res;
      private TabHost tabHost;
      private TabHost.TabSpec spec;  // Resusable TabSpec for each tab
      private Intent intent;  // Reusable Intent for each tab
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // The IDs in main2 should be exactly like the current ones.
          setContentView(R.layout.main2);
  
          res = getResources(); // Resource object to get Drawables
          tabHost = getTabHost();  // The activity TabHost
          // Create an Intent to launch an Activity for the tab (to be reused)
          intent = new Intent().setClass(this, Tab1.class);
  
          // Initialize a TabSpec for each tab and add it to the TabHost
          spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                            res.getDrawable(R.drawable.ic_tab_artists_grey))
                        .setContent(intent);
          tabHost.addTab(spec);
  
          // Do the same for the other tabs
          intent = new Intent().setClass(this, Tab2.class);
          spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                            res.getDrawable(R.drawable.ic_tab_albums_grey))
                        .setContent(intent);
          tabHost.addTab(spec);
  
          intent = new Intent().setClass(this, Tab3.class);
          spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                            res.getDrawable(R.drawable.ic_tab_songs_grey))
                        .setContent(intent);
          tabHost.addTab(spec);
  
          tabHost.setCurrentTab(1);
      }
  }
  </code></pre>
  
  <p>The main xml:</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
              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>The selector for the first tab:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;!-- When selected, use grey --&gt;
      &lt;item android:drawable="@drawable/ic_tab_artists_grey"
            android:state_selected="true" /&gt;
      &lt;!-- When not selected, use white--&gt;
      &lt;item android:drawable="@drawable/ic_tab_artists_white"/&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <p>The selector for the second tab:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;!-- When selected, use grey --&gt;
      &lt;item android:drawable="@drawable/ic_tab_songs_grey"
            android:state_selected="true" /&gt;
      &lt;!-- When not selected, use white--&gt;
      &lt;item android:drawable="@drawable/ic_tab_songs_white"/&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <p>etc. My images are called ic_tab_songs_white, ic_tab_songs_grey, ic_tab_albums_white, ic_tab_albums_grey, ic_tab_artists_white, ic_tab_artists_grey.</p>