“iPhonish” Tabs
Oct 25, 2009
Let me introduce my custom tabs recipe: custom tabs layout with RadioGroup widget and hidden TabWidget view. Layout file can looks like this:
<?xml version="1.0" encoding="utf-8"?>
<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">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1" android:padding="20dip" android:background="#fff"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:checkedButton="@+id/first" android:id="@+id/states">
<RadioButton android:id="@+id/first" android:background="@drawable/button_radio"
android:width="80dip" android:height="70dip" />
<RadioButton android:id="@+id/second" android:background="@drawable/button_radio"
android:width="80dip" android:height="70dip" />
<RadioButton android:id="@+id/third" android:background="@drawable/button_radio"
android:width="80dip" android:height="70dip" />
<RadioButton android:id="@+id/fourth" android:background="@drawable/button_radio"
android:width="80dip" android:height="70dip" />
</RadioGroup>
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0" android:visibility="gone" />
</LinearLayout>
</TabHost>
There is no way to setup button drawable of RadioButton widget in layout XML file, so we should do it at Activity create event:
private void setupUI() {
RadioButton rbFirst = (RadioButton) findViewById(R.id.first);
RadioButton rbSecond = (RadioButton) findViewById(R.id.second);
RadioButton rbThird = (RadioButton) findViewById(R.id.third);
RadioButton rbFourth = (RadioButton) findViewById(R.id.fourth);
rbFirst.setButtonDrawable(R.drawable.ebay);
rbSecond.setButtonDrawable(R.drawable.flickr);
rbThird.setButtonDrawable(R.drawable.skype);
rbFourth.setButtonDrawable(R.drawable.you_tube);
RadioGroup rg = (RadioGroup) findViewById(R.id.states);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, final int checkedId) {
switch (checkedId) {
case R.id.first:
getTabHost().setCurrentTab(0);
break;
case R.id.second:
getTabHost().setCurrentTab(1);
break;
case R.id.third:
getTabHost().setCurrentTab(2);
break;
case R.id.fourth:
getTabHost().setCurrentTab(3);
break;
}
}
});
}
Final result:

And as usual there are source code and executable file.
fazevedo
November 2nd, 2009 at 12:49 #
Good technique. Well done and keep it up!
PhaniJ
November 18th, 2009 at 03:26 #
Thanks for the awesome idea
Kyle Kuepker
December 21st, 2009 at 06:08 #
Android newbie here.….could your recipe be modified to make vertical oriented tabs on the left side of the screen?
grbrg
April 13th, 2010 at 14:13 #
This is a rather nice solution! However, how do you handle different screen resolutions? The XML file seems to require constant values for each radio buttons width…
theo
August 9th, 2010 at 16:38 #
looks great, thanks!
dirk
August 20th, 2010 at 00:18 #
I scoured the web for solutions to custom tabs and this is by far the simplest.
Ray
September 20th, 2010 at 07:44 #
@grbrg The xml for the radio buttons height and width uses dip, which means the value scales with the density of the screen, ie, on a normal screen, 1 dip = 1 pixel, but on a high density screen 1 dip = 1.5 pixels.
See here for more info: