TabActivity:
In android, Using tabs, we can open multiple activities within single screen.
Three ways to create Tabs.
1. extends class from TabActivity or ActivityGroup
2. extending Activity and use LocalActivityManager
3. using fragments ( Api 11 required).
Here is the sample working code for Tabs
package com.srinivas.tabs;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.widget.TabHost;
public class MainActivity extends Activity {
private static TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
tabHost = (TabHost) findViewById(R.id.tabhost);
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);
Resources res = getResources();
TabHost.TabSpec spec;
Intent songintent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs")
.setIndicator("Songs", res.getDrawable(R.drawable.ic_launcher))
.setContent(logintent);
tabHost.addTab(spec);
Intent videointent = new Intent().setClass(this, VideosActivity.class);
spec = tabHost
.newTabSpec("video")
.setIndicator("Videos", res.getDrawable(R.drawable.ic_launcher))
.setContent(viodeintent);
tabHost.addTab(spec);
Intent booksintent = new Intent().setClass(this, BooksActivity.class);
spec = tabHost.newTabSpec("books")
.setIndicator("Books", res.getDrawable(R.drawable.ic_launcher))
.setContent(booksintent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
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="fill_parent"
android:padding="5dp"
/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</TabHost>
if you want to place Tabs at top , remove android:layout_alignParentBottom="true"
https://picasaweb.google.com/115546358500228747299/Jun27201203?authkey=Gv1sRgCLmos6S5s_iaTA&feat=directlink#5836330903891211490
In android, Using tabs, we can open multiple activities within single screen.
Three ways to create Tabs.
1. extends class from TabActivity or ActivityGroup
2. extending Activity and use LocalActivityManager
3. using fragments ( Api 11 required).
Here is the sample working code for Tabs
package com.srinivas.tabs;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.widget.TabHost;
public class MainActivity extends Activity {
private static TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
tabHost = (TabHost) findViewById(R.id.tabhost);
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);
Resources res = getResources();
TabHost.TabSpec spec;
Intent songintent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs")
.setIndicator("Songs", res.getDrawable(R.drawable.ic_launcher))
.setContent(logintent);
tabHost.addTab(spec);
Intent videointent = new Intent().setClass(this, VideosActivity.class);
spec = tabHost
.newTabSpec("video")
.setIndicator("Videos", res.getDrawable(R.drawable.ic_launcher))
.setContent(viodeintent);
tabHost.addTab(spec);
Intent booksintent = new Intent().setClass(this, BooksActivity.class);
spec = tabHost.newTabSpec("books")
.setIndicator("Books", res.getDrawable(R.drawable.ic_launcher))
.setContent(booksintent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
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="fill_parent"
android:padding="5dp"
/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</TabHost>
if you want to place Tabs at top , remove android:layout_alignParentBottom="true"
https://picasaweb.google.com/115546358500228747299/Jun27201203?authkey=Gv1sRgCLmos6S5s_iaTA&feat=directlink#5836330903891211490
0 coment�rios:
Post a Comment