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
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | 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
01 02 03 04 05 06 07 08 09 10 11 12 | 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | 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
01 02 03 04 05 06 07 08 09 10 11 12 | 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | 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
01 02 03 04 05 06 07 08 09 10 11 12 | 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <? xml version = "1.0" encoding = "utf-8" ?> 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <? xml version = "1.0" encoding = "utf-8" ?> 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <? xml version = "1.0" encoding = "utf-8" ?> 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | <? xml version = "1.0" encoding = "utf-8" ?> 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <? xml version = "1.0" encoding = "utf-8" ?> 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | <? xml version = "1.0" encoding = "utf-8" ?> 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <? xml version = "1.0" encoding = "utf-8" ?> 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | <? xml version = "1.0" encoding = "utf-8" ?> 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <? xml version = "1.0" encoding = "utf-8" ?> 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.