Android Core
Android Child Group Activity
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:
- Create a new project, name TabGroupChildDemo.
- Create an TabHostActivity and extend it to TabActivity.
- Create 3 other activity name-HomeActivity, AboutActivity, ContactActivity.
- Create 3 group activity name-HomeGroupActivity, AboutGroupActivity, ContactGroupActivity.
- Create an other Activity ChildActivity.java.
- Create 4 layout for Home, About, Contact, Child Activity, name activity_home, activity_about, activity_contact, activity_child.
- Create 3 other layout for display tabs- home_tab,about_tab,contact_tab.
- Add activity and group activity in manifest.xml
- Add images – home.png, about.png, contact.png, ic_tab_background in drawable folder, download below images
My Code:
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; } } } }
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); } }
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)); } }
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); } }
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)); } }
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); } }
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)); } }
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); } }
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>
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>
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>
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>
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>
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>
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>
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>
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>
You can refer below link for original post for “simple tab host activity” – http://www.androidhub4you.com/2013/04/android-tabactivity-tab-layout-demo-tab.html
Reference: Android Child Group Activity from our JCG partner Manish Srivastava at the Android Hub 4 you blog.