Wednesday, April 10, 2013

Android Child Group Activity | Android TabActivity | Tab Host Activity Example in Android | Activity inside Activity in Android | Animated Activity in Android

Hello Friends,
 Today I am going to share very Important post for Child Group Activity, means activity open inside Tab Group. For this I use Animated Activity. This logic I picked from stackoverflow.com. Hope it will help some one.

Print Screen:






1)Create a new project, name TabGroupChildDemo.
2)Create an  TabHostActivity and extend it to TabActivity.
3)Create 3 other activity name-HomeActivity, AboutActivity, ContactActivity.
4)Create 3 group activity name-HomeGroupActivity, AboutGroupActivity, ContactGroupActivity.
5)Create an other Activity ChildActivity.java.
6)Create 4 layout for Home, About, Contact, Child Activity, name activity_home, activity_about, activity_contact, activity_child.
7)Create 3 other layout for display tabs- home_tab,about_tab,contact_tab.
8)Add activity and group activity in manifest.xml
9)7)Add images - home.png, about.png, contact.png,ic_tab_background in drawable folder, download below images-

 My Code:

1)TabHostActivity.java

package com.manish.tabdemo;

import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;



@SuppressWarnings("deprecation")
public class TabHostActivity extends TabActivity implements OnTabChangeListener
{
private static final String[] TABS = { "HomeGroupActivity", "AboutGroupActivity", "ContactGroupActivity" };
private static final String[] TAB_NAMES = { "Home", "About", "Contact"};
public static TabHost tabs ;
    public static TabWidget tabWidget ;    
protected Bitmap roundedImage;
    public boolean checkTabsListener = false;

    
    public void onCreate(Bundle icicle)
    {        
    super.onCreate(icicle);
        setContentView(R.layout.activity_tab_host);
      
        Bitmap roundedImage = BitmapFactory.decodeResource(getResources(),R.drawable.ic_tab_background);
        roundedImage = getRoundedCornerBitmap(roundedImage,3);
       
        tabs = getTabHost();
 
        tabWidget = tabs.getTabWidget();
                      
   tabs.setOnTabChangedListener(this);
        
   for (int i = 0; i < TABS.length; i++)
        {
        TabHost.TabSpec tab = tabs.newTabSpec(TABS[i]);
       
        //Asociating Components
        ComponentName oneActivity = new ComponentName("com.manish.tabdemo", "com.manish.tabdemo." + TABS[i]);
        Intent intent = new Intent().setComponent(oneActivity);          
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        tab.setContent(intent);        
        //Setting the Indicator
        MyTabIndicator myTab = new MyTabIndicator(this, TAB_NAMES[i],(i+1), roundedImage); 
        tab.setIndicator(myTab);
        tabs.addTab(tab);
        }
        
   checkTabsListener = true;
   
        for(int i=0;i<tabs.getTabWidget().getChildCount();i++)
        {
        tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
        }
        
     
tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_tab_background);  

//Maintaining Clicks

// Home Tab Click

tabWidget.getChildAt(0).setOnClickListener(new OnClickListener()
        {
@Override
public void onClick(View v)
{  
if(HomeGroupActivity.HomeGroupStack != null && HomeGroupActivity.HomeGroupStack.mIdList.size()>1)
{  
HomeGroupActivity.HomeGroupStack.getLocalActivityManager().removeAllActivities();
HomeGroupActivity.HomeGroupStack.mIdList.clear();
HomeGroupActivity.HomeGroupStack.mIntents.clear();
HomeGroupActivity.HomeGroupStack.mAnimator.removeAllViews();
HomeGroupActivity.HomeGroupStack.startChildActivity("CareGroupActivity", new Intent(HomeGroupActivity.HomeGroupStack, HomeActivity.class));
finish();
}
 
tabWidget.setCurrentTab(0);
tabs.setCurrentTab(0);
tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_tab_background);  
}
        });


// About tab Click

tabWidget.getChildAt(1).setOnClickListener(new OnClickListener()
        {
public void onClick(View v)
{  
if(AboutGroupActivity.AboutGroupStack != null && AboutGroupActivity.AboutGroupStack.mIdList.size()>0)
{
AboutGroupActivity.AboutGroupStack.getLocalActivityManager().removeAllActivities();
AboutGroupActivity.AboutGroupStack.mIdList.clear();  
AboutGroupActivity.AboutGroupStack.mIntents.clear();
AboutGroupActivity.AboutGroupStack.mAnimator.removeAllViews();  
AboutGroupActivity.AboutGroupStack.startChildActivity("TrackingGroupActivity", new Intent(AboutGroupActivity.AboutGroupStack, AboutActivity.class));
}  

tabWidget.setCurrentTab(1);
tabs.setCurrentTab(1);
tabs.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_tab_background);  
}
        });

// Contact tab click

tabWidget.getChildAt(2).setOnClickListener(new OnClickListener()
        {
public void onClick(View v)
{  
if(ContactGroupActivity.ContactGroupStack != null && ContactGroupActivity.ContactGroupStack.mIdList.size()>0)
{
 
ContactGroupActivity.ContactGroupStack.getLocalActivityManager().removeAllActivities();
ContactGroupActivity.ContactGroupStack.mIdList.clear();  
ContactGroupActivity.ContactGroupStack.mIntents.clear();
ContactGroupActivity.ContactGroupStack.mAnimator.removeAllViews();  
ContactGroupActivity.ContactGroupStack.startChildActivity("DashboardGroupActivity", new Intent(ContactGroupActivity.ContactGroupStack, ContactActivity.class));
}  

tabWidget.setCurrentTab(2);
tabs.setCurrentTab(2);
tabs.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_tab_background);  
}
        });


    }

    
    public class MyTabIndicator extends LinearLayout 
    {
public MyTabIndicator(Context context, String label, int tabId, Bitmap bgImg)
{
super(context);
LinearLayout tab = null;
TextView tv;
this.setGravity(Gravity.CENTER);

if(tabId == 1)
{
tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.home_tab, null);
tv = (TextView)tab.findViewById(R.id.tab_label);
tv.setText(label);
}



else if(tabId == 2)
{
tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.about_tab, null);
tv = (TextView)tab.findViewById(R.id.tab_label);
tv.setText(label);
}
else if(tabId == 3)
{
tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contact_tab, null);
tv = (TextView)tab.findViewById(R.id.tab_label);
tv.setText(label);
}

this.addView(tab, new LinearLayout.LayoutParams(320/4,55));
}
    }

    
    
public void onTabChanged(String tabId) 
{  
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(tabs.getApplicationWindowToken(), 0);
       
        for(int i=0; i<tabs.getTabWidget().getChildCount(); i++)
{                        
        if(tabId.equalsIgnoreCase(TABS[i]))
{
tabs.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_tab_background);  
}
else
{
tabs.getTabWidget().getChildAt(i).setBackgroundColor((Color.TRANSPARENT));
}  
  }
}

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPxRadius)
{
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx =roundPxRadius;
     
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
     
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
     
        return output;
}

public void onResume()
{
super.onResume();


//ReConstructing TabViews
reDesignTabViews();
}

public void onPause()
{
super.onPause();
}




/**
* Method used to re constructing the Views at tab bar. This solves tabs disappearing issue.
*/
public void reDesignTabViews()
{
MyTabIndicator myIndicator;


//Construction of tab views....
for(int i=0 ; i< tabWidget.getChildCount() ; i++)
{
myIndicator = (MyTabIndicator) tabWidget.getChildAt(i);
myIndicator.removeAllViews();

switch (i) 
{

case 0:
myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.home_tab, null));
break;
case 1:
myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.about_tab, null));
break;
case 2:
myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.contact_tab, null));
break;

}
}
}

}

2)HomeActivity.java

package com.manish.tabdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.manish.util.AnimatedActivity;

public class HomeActivity extends Activity {
Button button1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

AnimatedActivity pActivity = (AnimatedActivity) HomeActivity.this
.getParent();
Intent intent = new Intent(HomeActivity.this,
ChildActivity.class);
pActivity.startChildActivity("home_screen", intent);
}
});
}

@Override
public void onBackPressed() {
System.out.println("***back*");
HomeActivity.super.onBackPressed();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("****event****" + event + "****" + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK) {

finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

3)HomeGroupActivity.java

package com.manish.tabdemo;

import android.content.Intent;
import android.os.Bundle;

import com.manish.util.AnimatedActivity;

public class HomeGroupActivity extends AnimatedActivity {
public static HomeGroupActivity HomeGroupStack;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HomeGroupStack = HomeGroupActivity.this;

startChildActivity("HomeGroupActivity", new Intent(this,
HomeActivity.class));
}
}

4)AboutActivity.java

package com.manish.tabdemo;

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

public class AboutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
}
}

5)AboutGroupActivity.java

package com.manish.tabdemo;

import android.content.Intent;
import android.os.Bundle;

import com.manish.util.AnimatedActivity;

public class AboutGroupActivity extends AnimatedActivity {
public static AboutGroupActivity AboutGroupStack;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AboutGroupStack = AboutGroupActivity.this;

startChildActivity("AboutGroupActivity", new Intent(this,
AboutActivity.class));
}
}

6)ContactActivity.java

package com.manish.tabdemo;

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

public class ContactActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
}
}

7)ContactGroupActivity.java

package com.manish.tabdemo;

import android.content.Intent;
import android.os.Bundle;

import com.manish.util.AnimatedActivity;

public class ContactGroupActivity extends AnimatedActivity {
public static ContactGroupActivity ContactGroupStack;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContactGroupStack = ContactGroupActivity.this;

startChildActivity("ContactGroupActivity", new Intent(this,
ContactActivity.class));
}
}

8)ChildActivity.java

package com.manish.tabdemo;

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

public class ChildActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
}
}

9)activity_tab_host.xml

<?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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="-4dp"
            android:layout_weight="0" />
    </LinearLayout>

</TabHost>

10)activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#0099CC" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Home Tab"
        android:textColor="#ffffff"
        android:textSize="35sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="Open Child" />

</RelativeLayout>

11)home_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dip"
        android:layout_marginTop="2dip"
        android:background="@drawable/home" >
    </TextView>

    <TextView
        android:id="@+id/tab_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home"
        android:textColor="#0099CC" />

</LinearLayout>

12)activity_about.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#808080" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="About Tab"
        android:textColor="#ffffff"
        android:textSize="35sp" />

</RelativeLayout>

13)about_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dip"
        android:layout_marginTop="2dip"
        android:background="@drawable/about" >
    </TextView>

    <TextView
        android:id="@+id/tab_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="About"
        android:textColor="#0099CC" />

</LinearLayout>

14)activity_contact.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffdddd" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Contact Tab"
        android:textColor="#000000"
        android:textSize="35sp" />

</RelativeLayout>

15)contact_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dip"
        android:layout_marginTop="2dip"
        android:background="@drawable/contact" >
    </TextView>

    <TextView
        android:id="@+id/tab_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Contact"
        android:textColor="#0099CC" />

</LinearLayout>

16)activity_child.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="#0099CC"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Child Activity"
        android:textSize="35sp"
        android:textColor="#ffffff" />

</RelativeLayout>

17)manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.tabdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.manish.tabdemo.TabHostActivity"
            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="com.manish.tabdemo.HomeActivity" />
        <activity android:name="com.manish.tabdemo.AboutActivity" />
        <activity android:name="com.manish.tabdemo.ContactActivity" />
        <activity android:name="com.manish.tabdemo.ChildActivity" />
        <activity android:name="com.manish.tabdemo.AboutGroupActivity" />
        <activity android:name="com.manish.tabdemo.ContactGroupActivity" />
        <activity android:name="com.manish.tabdemo.HomeGroupActivity" />
    </application>

</manifest>




18)DOWNLOAD ZIP CODE



Please leave your comment and suggestion below-

Thanks,

you can refer below post for simple tab host activity- http://www.androidhub4you.com/2013/04/android-tabactivity-tab-layout-demo-tab.html

Tuesday, April 9, 2013

Countup demo in Android | Custom timer example in Android | Start, Pause, Stop digital watch code in Android | Java example for Watch

Hello Friends,

Today I am going to share very Important code for countdown watch in Android. For this I am using Handler Thread. Hope it will help you guys. Just copy paste below code and enjoy.

1)TimerDemo.java
package com.example.timerdemoandroid;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TimerDemo extends Activity {
/** Called when the activity is first created. */
private TextView textTimer;
private Button startButton;
private Button pauseButton;
private long startTime = 0L;
private Handler myHandler = new Handler();
long timeInMillies = 0L;
long timeSwap = 0L;
long finalTime = 0L;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer_demo);

textTimer = (TextView) findViewById(R.id.textTimer);

startButton = (Button) findViewById(R.id.btnStart);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);

}
});

pauseButton = (Button) findViewById(R.id.btnPause);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);

}
});

}

private Runnable updateTimerMethod = new Runnable() {

public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;

int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText("" + minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
}

};

}

2)activity_timer_demo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textTimer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnPause"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="37dp"
        android:textSize="40sp"
        android:textColor="#ffffff"
        android:text="00:00:00" />

    <Button
        android:id="@+id/btnPause"
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/btnStart"
        android:text="Pause" />

    <Button
        android:id="@+id/btnStart"
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="48dp"
        android:text="Start" />

</RelativeLayout>



Please put your comment..
Thanks,

Monday, April 1, 2013

Android TabActivity | Tab Layout Demo | Tab Host Activity Example in Android

Hi Friends,

          As we know Tab-Activity is now depreciated  in Android, but some time we use it for create simple Tab pages in our application. So today I am going to share tutorial for Tab Host Activity in android. It is a simple tab activity demo, group child activity and pager tab example I will share soon. Hope my blog help you. Please follow step by step my blog for create simple Tab Layout-

Print Screen: 




1)Create a new project, name TabHostDemo.
2)Create an  TabHostActivity and extend it to TabActivity.
3)Create 3 other activity name-Homeactivity, AboutActivity, ContactActivity.
4)Create layout activity_tab_host.xml .
5)Create another 3 layout for Home, About, Contact Activity, name activity_home, activity_about, activity_contact.
6)Do optional activity for change tab images on selection.Create ic_tab_home, ic_tab_about, ic_tab_contact.
Note-this step is not must, you can direct put your images in your TabHostActivity-
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, HomeActivity.class);
spec = tabHost.newTabSpec("home")
.setIndicator("HOME", res.getDrawable(R.drawable.home))//set here
.setContent(intent);
tabHost.addTab(spec);
7)Add images - home.png, about.png, contact.png in drawable download below images-






8)Add activity in manifest.xml
9)Run your project and enjoy :)

My Code:

1)TabHostActivity.java-
package com.manish.tabdemo;

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

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

Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, HomeActivity.class);
spec = tabHost.newTabSpec("home")
.setIndicator("HOME", res.getDrawable(R.drawable.ic_tab_home))
.setContent(intent);
tabHost.addTab(spec);

// Do the same for the other tabs

intent = new Intent().setClass(this, AboutActivity.class);
spec = tabHost.newTabSpec("about")
.setIndicator("ABOUT", res.getDrawable(R.drawable.ic_tab_about))
.setContent(intent);
tabHost.addTab(spec);


intent = new Intent().setClass(this, ContactActivity.class);
spec = tabHost
.newTabSpec("contact")
.setIndicator("CONTACT",
res.getDrawable(R.drawable.ic_tab_contact))
.setContent(intent);
tabHost.addTab(spec);

//set tab which one you want open first time 0 or 1 or 2
tabHost.setCurrentTab(0);


}

}

2)HomeActivity.java-

package com.manish.tabdemo;

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

public class HomeActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}

3)AboutActivity.java-

package com.manish.tabdemo;

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

public class AboutActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
}
}

4)ContactActivity.java-

package com.manish.tabdemo;

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

public class ContactActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
}
}

5)activity_tab_host.xml

<?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: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="0dip"
            android:layout_weight="1"/>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:layout_marginBottom="-4dp"/>
     
    </LinearLayout>

</TabHost>

6)activity_home.xml-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="#0099CC"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Home Tab"
        android:textSize="35sp"
        android:textColor="#ffffff" />

</RelativeLayout>

7)activity_about.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="#808080"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="About Tab"
        android:textSize="35sp"
        android:textColor="#ffffff" />

</RelativeLayout>

8)activity_contact.xml-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="#ffdddd"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Contact Tab"
        android:textSize="35sp"
        android:textColor="#000000" />

</RelativeLayout>

now put selector for tabs images in drawable folder- 

9)ic_tab_home.xml-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected -->
    <item android:drawable="@drawable/home"
          android:state_selected="true" />
    <!-- When not selected-->
    <item android:drawable="@drawable/home2" />
</selector>

10)ic_tab_about.xml-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected -->
    <item android:drawable="@drawable/about"
          android:state_selected="true" />
    <!-- When not selected-->
    <item android:drawable="@drawable/about2" />
</selector>

11)ic_tab_contact.xml-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected -->
    <item android:drawable="@drawable/contact"
          android:state_selected="true" />
    <!-- When not selected-->
    <item android:drawable="@drawable/contact2" />
</selector>

12)AndroidManifest.xml-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.tabdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.manish.tabdemo.TabHostActivity"
            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="com.manish.tabdemo.HomeActivity"/>
             <activity android:name="com.manish.tabdemo.AboutActivity"/>
                  <activity android:name="com.manish.tabdemo.ContactActivity"/>
    </application>

</manifest>

Please feel free to post your testimonials regarding my work here. Please leave your review by clicking on comment below this post.

Thanks,

you can refer below post for child group tab-
http://www.androidhub4you.com/2013/04/android-child-group-activity-android.html