TabHost without extending TabActivity...Example

TabDemo.java
package com.hcl.tab;
import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;

public class TabDemo extends ActivityGroup
{  
 TabHost host;
   /** Called when the activity is first created. */

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      Log.i(this.toString(), "OnCreate");

      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      Log.i(this.toString(), "get tab host");
       
      this.host = (TabHost) findViewById(R.id.tabhost);
      this.host.setup(getLocalActivityManager());
      host.setup();

      Log.i(this.toString(), "add tabs");
      host.addTab(host.newTabSpec("one")
                      .setIndicator("First Results")
                      .setContent(new Intent(this, FirstResultsListViewActivity.class)));

      host.addTab(host.newTabSpec("two")
                      .setIndicator("Second Results")
                      .setContent(new Intent(this, SecondResultsListViewActivity.class)));

      Log.i(this.toString(), "adjust tab size");
      host.getTabWidget().getChildAt(0).getLayoutParams().height = 35;
      host.getTabWidget().getChildAt(1).getLayoutParams().height = 35;

   }// end onCreate

}

FirstResultsListViewActivity.java

package com.hcl.tab;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class FirstResultsListViewActivity extends Activity{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.name);
  TextView tv=(TextView)findViewById(R.id.textView1);
  tv.setText("Madhu First activity");
 }
}

SecondResultsListViewActivity .java
package com.hcl.tab;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondResultsListViewActivity extends Activity{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.second);
  TextView tv=(TextView)findViewById(R.id.textView2);
  tv.setText("Madhu Second activity");
 }

}

manifest file xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.hcl.tab"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TabDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:name=".FirstResultsListViewActivity"/>
<activity android:name=".SecondResultsListViewActivity"/>             
       
    </application>
</manifest>

main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+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">
  <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>

name.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:text="First activity" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>



second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:text="Second activity" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>

Comments

  1. Thanks ..it helped me in my work

    ReplyDelete
  2. This tab activity information is good it helped me I used in my application.... It worked fine using Activitygroup...Thanks Maddy...

    ReplyDelete
  3. Enjoy madi..............@sunitha

    ReplyDelete
  4. How can i get reference of textview2 of SecondResultsListViewActivity inside main activity(TabDemo)

    ReplyDelete

Post a Comment

Please post comments here:-)

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams

SCJP1.6 Question and Answers