Home | History | Annotate | Download | only in views
      1 page.title=Hello, TabWidget
      2 parent.title=Hello, Views
      3 parent.link=index.html
      4 @jd:body
      5 
      6 <p>A {@link android.widget.TabWidget} offers the ability to easily draw an interface that uses
      7 tabs to navigate between different views.</p>
      8 
      9 <ol>
     10   <li>Start a new project/Activity called HelloTabWidget.</li>
     11   <li>Open the layout file and make it like so:</li>
     12   <pre>
     13 &lt;?xml version="1.0" encoding="utf-8"?>
     14 &lt;TabHost xmlns:android="http://schemas.android.com/apk/res/android"
     15     android:id="@android:id/tabhost"
     16     android:layout_width="fill_parent"
     17     android:layout_height="fill_parent">
     18     &lt;LinearLayout
     19         android:orientation="vertical"
     20         android:layout_width="fill_parent"
     21         android:layout_height="fill_parent">
     22         &lt;TabWidget
     23             android:id="@android:id/tabs"
     24             android:layout_width="fill_parent"
     25             android:layout_height="wrap_content" />
     26         &lt;FrameLayout
     27             android:id="@android:id/tabcontent"
     28             android:layout_width="fill_parent"
     29             android:layout_height="fill_parent">
     30             &lt;TextView 
     31                 android:id="@+id/textview1"
     32                 android:layout_width="fill_parent"
     33                 android:layout_height="fill_parent" 
     34                 android:text="this is a tab" />
     35             &lt;TextView 
     36                 android:id="@+id/textview2"
     37                 android:layout_width="fill_parent"
     38                 android:layout_height="fill_parent" 
     39                 android:text="this is another tab" />
     40             &lt;TextView 
     41                 android:id="@+id/textview3"
     42                 android:layout_width="fill_parent"
     43                 android:layout_height="fill_parent" 
     44                 android:text="this is a third tab" />
     45     	&lt;/FrameLayout>
     46     &lt;/LinearLayout>
     47 &lt;/TabHost>
     48 </pre>
     49     <p>Here, we've created a {@link android.widget.TabHost} that contains the entire layout of the Activity.
     50     A TabHost requires two descendant elements: a {@link android.widget.TabWidget} and a {@link android.widget.FrameLayout}.
     51     In order to properly layout these elements, we've put them inside a vertical {@link android.widget.LinearLayout}.
     52     The FrameLayout is where we keep the content that will change with each tab. Each child in the FrameLayout will
     53     be associated with a different tab.
     54     In this case, each tab simply shows a different {@link android.widget.TextView} with some text. </p>
     55     <p>Notice that the TabWidget and the FrameLayout elements have specific <code>android</code> namespace IDs. These are necessary
     56     so that the TabHost can automatically retrieve references to them, populate the TabWidget with the tabs that we'll define
     57     in our code, and swap the views in the FrameLayout. We've also defined our own IDs for each TextView, which we'll use to 
     58     associate each tab with the view that it should reveal.</p>
     59     <p>Of course, you can 
     60     make these child views as large as complex as you'd like &mdash; instead of the TextView elements, 
     61     you could start with other layout views and build a unique layout hierarchy for each tab.</p>
     62   </li>
     63   <li>Now we'll add our code. Open HelloTabWidget.java and make it a <code>TabActivity</code>.
     64     <p>By default, Eclipse creates a class that extends <code>Activity</code>. Change it to 
     65     extend <code>TabActivity</code>:</p>
     66     <pre>
     67 public class HelloTabWidget extends TabActivity {
     68 </pre>  
     69   </li>
     70   <li>Now fill in the the <code>onCreate</code> method like this:
     71   <pre>
     72 public void onCreate(Bundle savedInstanceState) {
     73     super.onCreate(savedInstanceState);
     74     setContentView(R.layout.main);
     75 
     76     mTabHost = getTabHost();
     77     
     78     mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.id.textview1));
     79     mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
     80     mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));
     81     
     82     mTabHost.setCurrentTab(0);
     83 }
     84 </pre>
     85     <p>As usual, we start by setting our layout.</p>
     86     <p>We then call the TabActivity method <code>getTabHost()</code>,
     87     which returns us a reference to the TabHost we created in our layout. Upon our TabHost, we call <code>addTab()</code>
     88     for each of the tabs that we want to add to the TabWidget. Each time we call this, we pass a 
     89     {@link android.widget.TabHost.TabSpec} that we build on the fly, and with it, chain together two necessary methods:
     90     <code>setIndicator()</code> to set the text for the tab button, and <code>setContent()</code> to define
     91     which View we want to associate with the tab and reveal when pressed. Our indicator is just a text string and 
     92     our content is an ID reference to the TextView elements we inserted in the FrameLayout.</p>
     93     <p>At the end, we call <code>setCurrentTab()</code> to define which tab should be opened by default. The tabs
     94     are saved like a zero-based array, so to open the first tab, we pass zero (<var>0</var>).</p>
     95   </li>
     96   <li>To clean-up the presentation a bit more, let's remove the window title that appears at the top of the layout.
     97   Android includes a theme that removes that title for us. To add it, open the Android Manifest file and add
     98   the <var>NoTitleBar</var> theme to the <code>&lt;application></code> tag. It should end up like this:
     99     <pre>
    100 &lt;application android:icon="&#64;drawable/icon" android:theme="&#64;android:style/Theme.NoTitleBar">
    101 </pre>
    102   </li>
    103   <li>That's it. Run your application.</li>
    104 
    105 </ol>
    106 
    107 
    108 <p>Your application should look like this:</p>
    109 <img src="images/hello-tabwidget.png" width="150px" />
    110 
    111 <div class="special"><p>You can include icons in your tabs by passing a 
    112 {@link android.graphics.drawable.Drawable} when you call <code>setIndicator()</code>. Here's an example
    113 that uses a Drawable created from an image in the project resources:</p>
    114 <pre>setIndicator("TAB 1", getResources().getDrawable(R.drawable.tab_icon))</pre>
    115 </div>
    116 
    117 <h3>References</h3>
    118 <ul>
    119 <li>{@link android.widget.TabWidget}</li>
    120 <li>{@link android.widget.TabHost}</li>
    121 <li>{@link android.widget.TabHost.TabSpec}</li>
    122 <li>{@link android.widget.FrameLayout}</li>
    123 </ul>
    124 
    125