Android Tech

The more we share, the more we get

TabView (Part 3) – Processes for each tab content in seperate activities


In the previous post, I have create TabView with detail controls in xml file, for example: Textview…In this post, I continue to guide you how to using activity to hold processing for each tab.

1. We will create a simple Tab control layout in XML file like this:

<?xml version=”1.0″ encoding=”utf-8″?>

<TabHost android:layout_width=”fill_parent”
android:layout_height=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@android:id/tabhost”>
<LinearLayout android:id=”@+id/LinearLayout01″
android:orientation=”vertical” android:layout_height=”fill_parent”
android:layout_width=”fill_parent”>
<TabWidget android:id=”@android:id/tabs”
android:layout_height=”wrap_content” android:layout_width=”fill_parent”></TabWidget>
<FrameLayout android:id=”@android:id/tabcontent”
android:layout_height=”fill_parent” android:layout_width=”fill_parent”></FrameLayout>
</LinearLayout>

</TabHost>

2. We’ll create 2 activities to demo this example like this:

Activity 1: Act1

package android.tab;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Act1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/* First Tab Content */
TextView textView = new TextView(this);
textView.setText(“This is tab content from Act1”);
setContentView(textView);

}
}

Activity 2: Act2

package android.tab;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Act2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/* First Tab Content */
TextView textView = new TextView(this);
textView.setText(“This is tab content from Act2”);
setContentView(textView);

}
}

3. Create tab and attach activities to each tab

package android.tab;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/* TabHost will have Tabs */
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
//tabHost.setup();

/* TabSpec used to create a new tab.
* By using TabSpec only we can able to setContent to the tab.
* By using TabSpec setIndicator() we can set name to tab. */

/* tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec firstTabSpec = tabHost.newTabSpec(“tid1”);
TabSpec secondTabSpec = tabHost.newTabSpec(“tid2”);

/* TabSpec setContent() is used to set content for a particular tab. */
Intent t1 = new Intent(this,Act1.class);
firstTabSpec.setContent(t1);
firstTabSpec.setIndicator(“First Tab Name”);

Intent t2 = new Intent(this,Act2.class);
secondTabSpec.setContent(t2);
secondTabSpec.setIndicator(“Second Tab Name”);

/* Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
}
}

Very important thing is that you have to use TabActivity in this case. I can’t run it with normal Activity. If somebody know how to do this. Please share for me. :d

And so, this is the final result:

You can download the source code from here

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: