Wednesday 22 February 2012

Android add Tabs from other xml Layouts

The big question which made fire my crazy is: how to add tabs to the TabHost from other xml layouts?

I searched and searched much times and finally I tried something and I had succeed.

The Step to have tabs from other xml layouts:
1- Add tab host to your layout -main_window-
2- Create your separate XML layout file
3- Inflate it to view in java code.
4- Override createTabContent and return the inflated view.
5- Add the view to the TabSpec.
Look @ the code bellow

setContentView(R.layout.main_window);
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View row = layoutInflater.inflate(R.layout.mySeperatedXMLLayout,null);
ListView leftList = (ListView) row.findViewById(R.id.leftList);
leftList .setAdapter(new IconAndTextListAdapter(getBaseContext(),
R.layout.icon_and_text, new Contents()
.getCountriesFlagsLinks("en"), new Contents()
.getCountriesNames("en"), R.id.icon_view,
R.id.text_view));

ListView categoriesList = (ListView) row.findViewById(R.id.rightList);
categoriesList.setAdapter(new IconAndTextListAdapter(getBaseContext(),
R.layout.icon_and_text, new Contents()
.getCountriesFlagsLinks("en"), new Contents()
.getCountriesNames("en"), R.id.icon_view,
R.id.text_view));

TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
TabSpec setContent = tabHost.newTabSpec("Tab1")
.setIndicator("Countries").setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return row;
}});
tabHost.addTab(setContent);


double_lists_holder.xml
// Comment

android:id="@+id/doubList_tbL"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/rock_bg"
android:stretchColumns="0" >



android:id="@+id/leftList"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_weight=".50"
android:cacheColorHint="#00000000"
android:divider="@drawable/list_divider"
android:dividerHeight="10dp"
android:drawSelectorOnTop="false"
android:listSelector="@drawable/free_ico_color5"
android:scrollbars="vertical"
android:soundEffectsEnabled="true" />

android:id="@+id/rightList"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_weight=".50"
android:cacheColorHint="#00000000"
android:drawSelectorOnTop="false"
android:listSelector="@drawable/paid_ico_color3"
android:scrollbars="vertical"
android:soundEffectsEnabled="true" />





main_window.xml
// Comment

android:layout_width="fill_parent"
android:layout_height="fill_parent" >

android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/googlAd_view"
android:layout_alignParentTop="true" >

android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >


android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >






Mohammad Abu Hmead

No comments:

Post a Comment