Android Programming Tutorials

(Romina) #1
Splitting the Tab

</TableLayout>
</FrameLayout>
</LinearLayout>
</TabHost>

Step #2: Wire In the Tabs....................................................................


Next, we need to modify the LunchList itself, so it is a TabActivity (rather


than a plain Activity) and teaches the TabHost how to use our FrameLayout


contents for the individual tab panes. To do this:



  1. Add imports to LunchList for android.app.TabActivity and
    android.widget.TabHost

  2. Make LunchList extend TabActivity

  3. Obtain 32px high icons from some source to use for the list and
    details tab icons, place them in LunchList/res/drawable as list.png
    and restaurant.png, respectively

  4. Add the following code to the end of your onCreate() method:


TabHost.TabSpec spec=getTabHost().newTabSpec("tag1");

spec.setContent(R.id.restaurants);
spec.setIndicator("List", getResources()
.getDrawable(R.drawable.list));
getTabHost().addTab(spec);

spec=getTabHost().newTabSpec("tag2");
spec.setContent(R.id.details);
spec.setIndicator("Details", getResources()
.getDrawable(R.drawable.restaurant));
getTabHost().addTab(spec);

getTabHost().setCurrentTab( 0 );

At this point, you can recompile and reinstall the application and try it out.


You should see a two-tab UI like this:


49
Free download pdf