Skip to content Skip to sidebar Skip to footer

Android: How To Build Tabs Like The Ones Show On Android Ui Page

So android goes out of its way to build this nice UI guide for everyone to use. But I don't see anywhere where it shows code examples of how to build these elements. The UI guid

Solution 1:

do something like this.

this is a full working code. enjoy

somewhere in oncreate method of activity extending Tabactivity

  tabHost = getTabHost();
  Intent intent;
  intent = newIntent().setClass(this, FirstActvity.class);
  setupTab("NearBy", intent, R.drawable.firsttabdrawable);
  intent = newIntent().setClass(this, SecondActivity.class);
  setupTab("History", intent, R.drawable.secondtabdrawable);
  intent = newIntent().setClass(this, ThirdActivity.class);
  setupTab("Setting", intent, R.drawable.thirdtabdrawable);

define setupTab methods as

privatevoidsetupTab(String tag, Intent intent, int selectorId) {
  ViewtabView= LayoutInflater.from(tabHost.getContext()).inflate(R.layout.view, null);
  tabView.setBackgroundResource(selectorId);
  TabSpecsetContent= tabHost.newTabSpec(tag).setIndicator(tabView).setContent(intent);
  tabHost.addTab(setContent);
  }

view.xml as

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"
></LinearLayout>

and firsttabdrawable.xml in drawable folder as

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><!-- When selected, use grey --><itemandroid:drawable="@drawable/selectedfirsttabimage"android:state_selected="true" /><!-- When not selected, use white--><itemandroid:drawable="@drawable/notselectedfirsttabimage" /></selector>

define secondtabdrawable.xml and thirddrawable.xml in the same way

Solution 2:

The tabs you need are part of the ActionBar. Specifically they are displayed when the ActionBar is in Navigation Tab mode.

http://developer.android.com/guide/topics/ui/actionbar.html (see under "Adding Navigation Tabs")

You may want to use www.ActionbarSherlock.com which is a library that will give you the ActionBar on nearly all versions of Android. It works the same as the official one, and includes the tabs.

Do not use the TabActivity any more, it's old and being deprecated. ActionBar is the future.

Post a Comment for "Android: How To Build Tabs Like The Ones Show On Android Ui Page"