|
|
Select a tab and see the related Activity. Layout file tab.xml contains the required TabHost TabWidget and FrameLayout elements. A TabHost object is created from the tab.xml element, "@android:id/tabhost". A new TabSpec object is created from the factory method newTabSpec(..) of the TabHost object. The indicator sets the tab name and icon. Two Intents are created for a basic Activity and an Activity defined as a layout. Activity2 is from this library at ButtonPress-LabelShow.
package com.brainyideas.dev.tba;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabBarApp extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
Resources res = getResources();
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
Intent intent1 = new Intent(this,ActivityOne.class);
TabSpec tabSpec1 = tabHost.newTabSpec("tab1");
tabSpec1.setIndicator("Tab One",res.getDrawable(R.drawable.tab1_icons));
tabSpec1.setContent(intent1);
tabHost.addTab(tabSpec1);
Intent intent2 = new Intent(this,ActivityTwo.class);
TabSpec tabSpec2 = tabHost.newTabSpec("tab2");
tabSpec2.setIndicator("Tab Two",res.getDrawable(R.drawable.tab2_icons));
tabSpec2.setContent(intent2);
tabHost.addTab(tabSpec2);
}
}
package com.brainyideas.dev.tba;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ActivityOne extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Activity One");
setContentView(textView);
}
}package com.brainyideas.dev.tba;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ActivityTwo extends Activity {
Button button1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
init();
}
void init() {
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
labelShow();
}
});
}
void labelShow() {
TextView log = (TextView) findViewById(R.id.textView1);
log.setText("Present at 11:31am");
}
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/tab1_selected"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/tab1_normal" />
</selector>
<?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:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> </TabHost>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/startvalue" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>