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:
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>
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
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
I've bookmarked this for my future reference. I think I like the way that you have set up your tabs, over the way I have been doing (I've been using AndroidHive's tutorials for the tabactivity). But I guess your way makes a lot more sense to me as a person.
ReplyDeleteThanks for sharing your idea. I have no more idea about AndroidHive's But I think my way is very good. Hope it will help you..
DeleteTabActivity has been deprecated for a long time. Also, bottom tabs are inappropriate on Android.
ReplyDeleteYes you are right we can use action bar, pager, menu etc. But some time really we need these things on client demand. And I always write my problems and experience so when I will work on Action bar then sure I will publish.
Deletecan you send me zip file.
ReplyDeleteammsalem@gmail.com
Hi Mathan Every thing is clear on my blog please copy paste the code..
DeleteWell I have sent you zip project please check..
Thanks,
Hey Manish,
ReplyDeleteIts nice example...
pls give me the source code.
umsahir307@gmail.com
Please don't mind just copy paste from above it is not much complicated..
DeleteThanks,
hello, I need your help on TabHost, I do not understand my TabHost when it was raised to enter data into.and I do not know how
ReplyDeleteI did not get where you got stuck? please clear your problem..
DeleteI would like to thank you, this is my first time talking to a foreigner. sorry for you because I can google talk incorrect.
ReplyDeleteMy problem is, when I add notes to EditText and TabHost that the bar is raised as shown.
https://www.facebook.com/photo.php?fbid=474228902657638&set=a.474228879324307.1073741837.100002115965372&type=3&theater
and the:
Deletehttps://www.facebook.com/photo.php?fbid=474228909324304&set=a.474228879324307.1073741837.100002115965372&type=3&theater
Hi I am unable to open your link its show This content is currently unavailable.
DeletePlease email me your png file at manishsri01@gmail.com
Thanks,
Thank you, I see you are very enthusiastic. I will mail to you, I will send you an email, thank you
DeleteHi..
ReplyDeletewill it work on 4.2 api version, actually i am using 4.2 api.
AnimatedActivity is inbuild in sdk or we have to create AnimatedActivity for import.
Actually AnimatedActivity is missing in my project. Please reply fast...my email: sohan.sharma339@gmail.com.
Thanks,
I have no idea about inbuild AnimatedActivity but you should create your own like above demo..
Deletehi .. can send me a zip file of your example ? thanks :D
ReplyDeleteattitude0107@hotmail.com
Please donwload it from GIT server-
Deletehttps://github.com/manishsri01/TabGroupChildDemo
I cant run the code... Error on animated Activity..
ReplyDeleteCan i know how to import the AnimatedActivty????
Please reply me:((
Sorry for delay...
DeletePlease download it from git server-
https://github.com/manishsri01/TabGroupChildDemo
I faced a problem . When I was in the child activity and then change the activity and then again return to home activity , application close down automatically .
ReplyDeletePlease Check now it will work, I fix that issue.
DeleteNow check-out new code from GIT server..
Thanks,
ActivityGroup,LocalActivityManager and TabActivity..these are deprecated classes.
ReplyDeleteis it possible to make a call or open a link in browser from the tab application
ReplyDeletewhen i pressed the home tab or switch the tab from others to home, it start the HomeActivity again . i do not want to get it restart every time i press the home tab. i want to launch the last activity(ChildActivity). how can i do that.
ReplyDeletecan i override back button in ChildActivity. i was trying to override back button but it was not working.
Hi Dileep, what you want to do exactly?
Delete1) suppose you are on child activity and you press android hardware home button, that mean activity running in background, so if you open again it it should open child activity.
2)If you go out using android hardware back button then ofcourse it will destroy your app and will start from home tab.
3)if you want open another tab every time you can set it. but this is not possible i.e.- you leave from which tab its start from there. for that case you should get current tab when you leave your application and on launch override your tab preference.
hi, i've a problem when i update the content of a tab, i don't want to refresh that! i want to UPDATE and mantain the content without reload that everytime! i don't know how to do that without use fragment and using your beautiful guide, so please tell me what i have to do!
ReplyDeletewhich content do you want update? do you have any list-view?
DeleteThis comment has been removed by the author.
ReplyDeleteIt is a really useful tutorial. Thanks. Even I have to use Tabhost and bottom tabs because of client's demand. I have one question though. I want to display back button on the action bar for child activity inside the ActivityGroup. How is that possible?
ReplyDeletei think yes actionbar should work. if not work you can create a top bar in your layout.
Deletecan you send me zip file.
ReplyDeleteahirvishnu@gmail.com
i have download on github but not imported properly so plz send me...
when you download any code from git-hub don't import just got through file menu in eclipse and then choose new option and after that from android option choose android project from existing code.
DeleteAbsolutely useful & truthfull demo till this date.
ReplyDeleteGood Keep it up buddy.
Thanks man!
DeleteThanks! Manish your post helped me a lot.
ReplyDeleteDoes the tabhost activity has to a launcher for this tutorial or it can be called from other activity with intent?
ReplyDeleteYou can use according to your requirement. You can make it launcher or open using intent from login or splash page.
Deletethank you for your example. But how use same example for a popup menu. My requirement is when click the first tab a popup menu is show also when select menu item show tha same tab on that page pls help meeeeeeeee
ReplyDeletethank
ReplyDeletevery well perfect but i couldnt start
ReplyDeleteIntent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Ops! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
} how start speec recogniton activity inside child activity any idea please
I am not sure please try to use instance of Activity/Application Context/ Context.
DeleteI couldnt do that
ReplyDeleteI write my HomeActivity
nothing writes nothing shows :S
when write speech recognition code to any standart mainactivity it works but in this nothin happens
i found this tutorials' tab activity very very useful and helpful but i couldnt work speech recognition code inside activities any help please
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ara);
searchedword = (EditText) findViewById(R.id.editText1);
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
searchedword.setText(globalVariable.getSomeVariable());
microphone = (ImageButton)findViewById(R.id.microphone);
microphone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(AraActivity.this, "I'm here", Toast.LENGTH_SHORT).show();
microphone = (ImageButton)findViewById(R.id.microphone);
microphone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(AraActivity.this, "I'm here", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Ops! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
searchedword.setText(text.get(0));
}
else{
searchedword.setText("??");
}
break;
}
}
}
Is there any way to do like that child activity from home tab remain unchanged after going to others tabs?
ReplyDeletethanks..
Is there any way to do like that child activity from home tab remain unchanged after going to others tabs?
ReplyDeletethanks..
How to do change tab1 to tab2 when i click button that are place in tab1 scrren.
ReplyDeleteSo what will be write code on tab1 button onClickListner().
If i use intent to pass activity that code run perfect but activity are open at the upper side of tab.so tab are not display.