Tutorial details

Android Child Group Activity | App Code for Sale | Preview

Android Child Group Activity | Android Tutorial

How to work with Child Group Activity

Overview PAGE TOP

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:

pag.JPG

  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 activityhome, activityabout, activitycontact, activitychild.
  7. Create 3 other layout for display tabs- hometab,abouttab,contact_tab.
  8. Add activity and group activity in manifest.xml
  9. Add images – home.png, about.png, contact.png, ictabbackground in drawable folder, download below images

pon.JPG

My Code:

TabHostActivity.java

001 package com.manish.tabdemo;
002 
003 import android.app.TabActivity;
004 import android.content.ComponentName;
005 import android.content.Context;
006 import android.content.Intent;
007 import android.graphics.Bitmap;
008 import android.graphics.Bitmap.Config;
009 import android.graphics.BitmapFactory;
010 import android.graphics.Canvas;
011 import android.graphics.Color;
012 import android.graphics.Paint;
013 import android.graphics.PorterDuff.Mode;
014 import android.graphics.PorterDuffXfermode;
015 import android.graphics.Rect;
016 import android.graphics.RectF;
017 import android.os.Bundle;
018 import android.view.Gravity;
019 import android.view.LayoutInflater;
020 import android.view.View;
021 import android.view.View.OnClickListener;
022 import android.view.inputmethod.InputMethodManager;
023 import android.widget.LinearLayout;
024 import android.widget.TabHost;
025 import android.widget.TabHost.OnTabChangeListener;
026 import android.widget.TabWidget;
027 import android.widget.TextView;
028 
029 
030 
031 @SuppressWarnings("deprecation")
032 public class TabHostActivity extends TabActivity implements OnTabChangeListener
033 {
034  private static final String[] TABS = { "HomeGroupActivity", "AboutGroupActivity","ContactGroupActivity" };
035  private static final String[] TAB_NAMES = { "Home", "About", "Contact"};
036  public static TabHost tabs ;
037     public static TabWidget tabWidget ;   
038  protected Bitmap roundedImage;
039     public boolean checkTabsListener = false;
040   
041      
042     public void onCreate(Bundle icicle)
043     {       
044      super.onCreate(icicle);
045         setContentView(R.layout.activity_tab_host);
046        
047         Bitmap roundedImage = BitmapFactory.decodeResource(getResources(),R.drawable.ic_tab_background);
048         roundedImage = getRoundedCornerBitmap(roundedImage,3);
049         
050         tabs = getTabHost();
051     
052         tabWidget = tabs.getTabWidget();
053                        
054      tabs.setOnTabChangedListener(this);
055          
056      for (int i = 0; i < TABS.length; i++)
057         {
058          TabHost.TabSpec tab = tabs.newTabSpec(TABS[i]);
059           
060          //Asociating Components
061          ComponentName oneActivity = new ComponentName("com.manish.tabdemo", "com.manish.tabdemo."+ TABS[i]);
062          Intent intent = new Intent().setComponent(oneActivity);          
063          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
064          tab.setContent(intent);        
065          //Setting the Indicator
066          MyTabIndicator myTab = new MyTabIndicator(this, TAB_NAMES[i],(i+1), roundedImage);
067          tab.setIndicator(myTab);
068          tabs.addTab(tab);
069         }
070          
071      checkTabsListener = true;
072       
073         for(int i=0;i<tabs.getTabWidget().getChildCount();i++)
074         {
075          tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
076         }
077          
078       
079   tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_tab_background);  
080    
081   //Maintaining Clicks
082    
083   // Home Tab Click
084    
085   tabWidget.getChildAt(0).setOnClickListener(new OnClickListener()
086         {  
087    @Override
088    public void onClick(View v)
089    {    
090      if(HomeGroupActivity.HomeGroupStack != null && HomeGroupActivity.HomeGroupStack.mIdList.size()>1)
091      {     
092       HomeGroupActivity.HomeGroupStack.getLocalActivityManager().removeAllActivities();
093       HomeGroupActivity.HomeGroupStack.mIdList.clear();
094       HomeGroupActivity.HomeGroupStack.mIntents.clear();
095       HomeGroupActivity.HomeGroupStack.mAnimator.removeAllViews();
096       HomeGroupActivity.HomeGroupStack.startChildActivity("CareGroupActivity", newIntent(HomeGroupActivity.HomeGroupStack, HomeActivity.class));
097       finish();
098      }
099       
100      tabWidget.setCurrentTab(0);
101      tabs.setCurrentTab(0);
102      tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_tab_background); 
103    }
104         });
105    
106   
107   // About tab Click
108    
109   tabWidget.getChildAt(1).setOnClickListener(new OnClickListener()
110         {  
111    public void onClick(View v)
112    {    
113     if(AboutGroupActivity.AboutGroupStack != null && AboutGroupActivity.AboutGroupStack.mIdList.size()>0)
114     {
115      AboutGroupActivity.AboutGroupStack.getLocalActivityManager().removeAllActivities();
116      AboutGroupActivity.AboutGroupStack.mIdList.clear();     
117      AboutGroupActivity.AboutGroupStack.mIntents.clear();
118      AboutGroupActivity.AboutGroupStack.mAnimator.removeAllViews();           
119      AboutGroupActivity.AboutGroupStack.startChildActivity("TrackingGroupActivity", newIntent(AboutGroupActivity.AboutGroupStack, AboutActivity.class));          
120     }    
121               
122     tabWidget.setCurrentTab(1);
123     tabs.setCurrentTab(1);
124     tabs.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_tab_background);      
125    }
126         });
127    
128   // Contact tab click
129    
130   tabWidget.getChildAt(2).setOnClickListener(new OnClickListener()
131         {  
132    public void onClick(View v)
133    {    
134     if(ContactGroupActivity.ContactGroupStack != null && ContactGroupActivity.ContactGroupStack.mIdList.size()>0)
135     {
136        
137      ContactGroupActivity.ContactGroupStack.getLocalActivityManager().removeAllActivities();
138      ContactGroupActivity.ContactGroupStack.mIdList.clear();     
139      ContactGroupActivity.ContactGroupStack.mIntents.clear();
140      ContactGroupActivity.ContactGroupStack.mAnimator.removeAllViews();           
141      ContactGroupActivity.ContactGroupStack.startChildActivity("DashboardGroupActivity", newIntent(ContactGroupActivity.ContactGroupStack, ContactActivity.class));          
142     }    
143               
144     tabWidget.setCurrentTab(2);
145     tabs.setCurrentTab(2);
146     tabs.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_tab_background);      
147    }
148         });
149    
150 
151     }
152    
153      
154     public class MyTabIndicator extends LinearLayout
155     {
156   public MyTabIndicator(Context context, String label, int tabId, Bitmap bgImg)
157   {
158    super(context);
159    LinearLayout tab = null;
160    TextView tv;
161    this.setGravity(Gravity.CENTER);
162     
163    if(tabId == 1)
164    {
165     tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.home_tab, null);
166     tv = (TextView)tab.findViewById(R.id.tab_label);
167     tv.setText(label);
168    }
169     
170     
171     
172    else if(tabId == 2)
173    {
174     tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.about_tab, null);
175     tv = (TextView)tab.findViewById(R.id.tab_label);
176     tv.setText(label);
177    }
178    else if(tabId == 3)
179    {
180     tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contact_tab, null);
181     tv = (TextView)tab.findViewById(R.id.tab_label);
182     tv.setText(label);
183    }
184         
185    this.addView(tab, new LinearLayout.LayoutParams(320/4,55));  
186   } 
187     }
188 
189      
190      
191  public void onTabChanged(String tabId)
192  {     
193   InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
194         imm.hideSoftInputFromWindow(tabs.getApplicationWindowToken(), 0);
195            
196         for(int i=0; i<tabs.getTabWidget().getChildCount(); i++)
197   {                            
198          if(tabId.equalsIgnoreCase(TABS[i]))
199    {           
200     tabs.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_tab_background);    
201    }
202    else
203    {
204     tabs.getTabWidget().getChildAt(i).setBackgroundColor((Color.TRANSPARENT));
205    }    
206     } 
207  }
208 
209   public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPxRadius)
210  {
211         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
212         Canvas canvas = new Canvas(output);
213          
214         final int color = 0xff424242;
215         final Paint paint = new Paint();
216         final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
217         final RectF rectF = new RectF(rect);
218         final float roundPx =roundPxRadius;
219       
220         paint.setAntiAlias(true);
221         canvas.drawARGB(0, 0, 0, 0);
222         paint.setColor(color);
223         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
224       
225         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
226         canvas.drawBitmap(bitmap, rect, rect, paint);
227       
228         return output;
229  }
230   
231  public void onResume()
232  {
233   super.onResume();
234    
235    
236   //ReConstructing TabViews
237   reDesignTabViews();
238  }
239   
240  public void onPause()
241  {
242   super.onPause();    
243  }
244   
245 
246    
247   
248  /**
249   * Method used to re constructing the Views at tab bar. This solves tabs disappearing issue.
250   */
251  public void reDesignTabViews()
252  {
253   MyTabIndicator myIndicator;
254    
255    
256   //Construction of tab views....
257   for(int i=0 ; i< tabWidget.getChildCount() ; i++)
258   {
259    myIndicator = (MyTabIndicator) tabWidget.getChildAt(i);
260    myIndicator.removeAllViews();
261     
262    switch (i)
263    {
264 
265      case 0:
266      myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.home_tab, null));
267      break;
268     case 1:   
269      myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.about_tab, null));   
270      break;
271     case 2:   
272      myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.contact_tab, null));   
273      break;
274      
275    }  
276   } 
277  }
278   
279 }

HomeActivity.java

01  package com.manish.tabdemo;
02  
03  import android.app.Activity;
04  import android.content.Intent;
05  import android.os.Bundle;
06  import android.view.KeyEvent;
07  import android.view.View;
08  import android.view.View.OnClickListener;
09  import android.widget.Button;
10  
11  import com.manish.util.AnimatedActivity;
12  
13  public class HomeActivity extends Activity {
14   Button button1;
15  
16    @Override
17   public void onCreate(Bundle savedInstanceState) {
18    super.onCreate(savedInstanceState);
19    setContentView(R.layout.activity_home);
20    button1 = (Button) findViewById(R.id.button1);
21    button1.setOnClickListener(new OnClickListener() {
22  
23      @Override
24     public void onClick(View v) {
25      // TODO Auto-generated method stub
26  
27       AnimatedActivity pActivity = (AnimatedActivity) HomeActivity.this
28        .getParent();
29      Intent intent = new Intent(HomeActivity.this,
30        ChildActivity.class);
31      pActivity.startChildActivity("home_screen", intent);
32     }
33    });
34   }
35  
36    @Override
37   public void onBackPressed() {
38    System.out.println("***back*");
39    HomeActivity.super.onBackPressed();
40   }
41  
42    @Override
43   public boolean onKeyDown(int keyCode, KeyEvent event) {
44    System.out.println("****event****" + event + "****" + keyCode);
45    if (keyCode == KeyEvent.KEYCODE_BACK) {
46  
47      finish();
48     return true;
49    }
50    return super.onKeyDown(keyCode, event);
51   }
52  }

HomeGroupActivity.java

01  package com.manish.tabdemo;
02  
03  import android.content.Intent;
04  import android.os.Bundle;
05  
06  import com.manish.util.AnimatedActivity;
07  
08  public class HomeGroupActivity extends AnimatedActivity {
09   public static HomeGroupActivity HomeGroupStack;
10  
11    @Override
12   public void onCreate(Bundle savedInstanceState) {
13    super.onCreate(savedInstanceState);
14    HomeGroupStack = HomeGroupActivity.this;
15  
16     startChildActivity("HomeGroupActivity", new Intent(this,
17      HomeActivity.class));
18   }
19  }

AboutActivity.java

01  package com.manish.tabdemo;
02  
03  import android.app.Activity;
04  import android.os.Bundle;
05  
06  public class AboutActivity extends Activity {
07   @Override
08   public void onCreate(Bundle savedInstanceState) {
09    super.onCreate(savedInstanceState);
10    setContentView(R.layout.activity_about);
11   }
12  }

AboutGroupActivity.java

01  package com.manish.tabdemo;
02  
03  import android.content.Intent;
04  import android.os.Bundle;
05  
06  import com.manish.util.AnimatedActivity;
07  
08  public class AboutGroupActivity extends AnimatedActivity {
09   public static AboutGroupActivity AboutGroupStack;
10  
11    @Override
12   public void onCreate(Bundle savedInstanceState) {
13    super.onCreate(savedInstanceState);
14    AboutGroupStack = AboutGroupActivity.this;
15  
16     startChildActivity("AboutGroupActivity", new Intent(this,
17      AboutActivity.class));
18   }
19  }

ContactActivity.java

01  package com.manish.tabdemo;
02  
03  import android.app.Activity;
04  import android.os.Bundle;
05  
06  public class ContactActivity extends Activity {
07   @Override
08   public void onCreate(Bundle savedInstanceState) {
09    super.onCreate(savedInstanceState);
10    setContentView(R.layout.activity_contact);
11   }
12  }

ContactGroupActivity.java

01  package com.manish.tabdemo;
02  
03  import android.content.Intent;
04  import android.os.Bundle;
05  
06  import com.manish.util.AnimatedActivity;
07  
08  public class ContactGroupActivity extends AnimatedActivity {
09   public static ContactGroupActivity ContactGroupStack;
10  
11    @Override
12   public void onCreate(Bundle savedInstanceState) {
13    super.onCreate(savedInstanceState);
14    ContactGroupStack = ContactGroupActivity.this;
15  
16     startChildActivity("ContactGroupActivity", new Intent(this,
17      ContactActivity.class));
18   }
19  }

ChildActivity.java

01  package com.manish.tabdemo;
02  
03  import android.app.Activity;
04  import android.os.Bundle;
05  
06  public class ChildActivity extends Activity {
07   @Override
08   public void onCreate(Bundle savedInstanceState) {
09    super.onCreate(savedInstanceState);
10    setContentView(R.layout.activity_child);
11   }
12  }

activitytabhost.xml

01  <?xml version="1.0" encoding="utf-8"?>
02  <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
03      android:id="@android:id/tabhost"
04      android:layout_width="fill_parent"
05      android:layout_height="fill_parent" >
06  
07      <LinearLayout
08          android:layout_width="fill_parent"
09          android:layout_height="fill_parent"
10          android:orientation="vertical" >
11  
12          <FrameLayout
13              android:id="@android:id/tabcontent"
14              android:layout_width="fill_parent"
15              android:layout_height="0dip"
16              android:layout_weight="1" />
17  
18          <TabWidget
19              android:id="@android:id/tabs"
20              android:layout_width="fill_parent"
21              android:layout_height="wrap_content"
22              android:layout_marginBottom="-4dp"
23              android:layout_weight="0" />
24      </LinearLayout>
25  
26  </TabHost>

activity_home.xml

01  <?xml version="1.0" encoding="utf-8"?>
02  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03      android:layout_width="fill_parent"
04      android:layout_height="fill_parent"
05      android:background="#0099CC" >
06  
07      <TextView
08          android:id="@+id/textView1"
09          android:layout_width="wrap_content"
10          android:layout_height="wrap_content"
11          android:layout_centerHorizontal="true"
12          android:layout_centerVertical="true"
13          android:text="Home Tab"
14          android:textColor="#ffffff"
15          android:textSize="35sp" />
16  
17      <Button
18          android:id="@+id/button1"
19          android:layout_width="wrap_content"
20          android:layout_height="wrap_content"
21          android:layout_below="@+id/textView1"
22          android:layout_centerHorizontal="true"
23          android:text="Open Child" />
24  
25  </RelativeLayout>

home_tab.xml

01  <?xml version="1.0" encoding="utf-8"?>
02  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03      android:layout_width="wrap_content"
04      android:layout_height="wrap_content"
05      android:layout_centerInParent="true"
06      android:gravity="center"
07      android:orientation="vertical" >
08  
09      <TextView
10          android:layout_width="wrap_content"
11          android:layout_height="wrap_content"
12          android:layout_marginBottom="1dip"
13          android:layout_marginTop="2dip"
14          android:background="@drawable/home" >
15      </TextView>
16  
17      <TextView
18          android:id="@+id/tab_label"
19          android:layout_width="wrap_content"
20          android:layout_height="wrap_content"
21          android:text="Home"
22          android:textColor="#0099CC" />
23  
24  </LinearLayout>

activity_about.xml

01  <?xml version="1.0" encoding="utf-8"?>
02  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03      android:layout_width="fill_parent"
04      android:layout_height="fill_parent"
05      android:background="#808080" >
06  
07      <TextView
08          android:id="@+id/textView1"
09          android:layout_width="wrap_content"
10          android:layout_height="wrap_content"
11          android:layout_centerHorizontal="true"
12          android:layout_centerVertical="true"
13          android:text="About Tab"
14          android:textColor="#ffffff"
15          android:textSize="35sp" />
16  
17  </RelativeLayout>

about_tab.xml

01  <?xml version="1.0" encoding="utf-8"?>
02  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03      android:layout_width="wrap_content"
04      android:layout_height="wrap_content"
05      android:layout_centerInParent="true"
06      android:gravity="center"
07      android:orientation="vertical" >
08  
09      <TextView
10          android:layout_width="wrap_content"
11          android:layout_height="wrap_content"
12          android:layout_marginBottom="1dip"
13          android:layout_marginTop="2dip"
14          android:background="@drawable/about" >
15      </TextView>
16  
17      <TextView
18          android:id="@+id/tab_label"
19          android:layout_width="wrap_content"
20          android:layout_height="wrap_content"
21          android:text="About"
22          android:textColor="#0099CC" />
23  
24  </LinearLayout>

activity_contact.xml

01  <?xml version="1.0" encoding="utf-8"?>
02  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03      android:layout_width="fill_parent"
04      android:layout_height="fill_parent"
05      android:background="#ffdddd" >
06  
07      <TextView
08          android:id="@+id/textView1"
09          android:layout_width="wrap_content"
10          android:layout_height="wrap_content"
11          android:layout_centerHorizontal="true"
12          android:layout_centerVertical="true"
13          android:text="Contact Tab"
14          android:textColor="#000000"
15          android:textSize="35sp" />
16  
17  </RelativeLayout>

contact_tab.xml

01  <?xml version="1.0" encoding="utf-8"?>
02  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03      android:layout_width="wrap_content"
04      android:layout_height="wrap_content"
05      android:layout_centerInParent="true"
06      android:gravity="center"
07      android:orientation="vertical" >
08  
09      <TextView
10          android:layout_width="wrap_content"
11          android:layout_height="wrap_content"
12          android:layout_marginBottom="1dip"
13          android:layout_marginTop="2dip"
14          android:background="@drawable/contact" >
15      </TextView>
16  
17      <TextView
18          android:id="@+id/tab_label"
19          android:layout_width="wrap_content"
20          android:layout_height="wrap_content"
21          android:text="Contact"
22          android:textColor="#0099CC" />
23  
24  </LinearLayout>

activity_child.xml

01  <?xml version="1.0" encoding="utf-8"?>
02  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03      android:layout_width="fill_parent"
04      android:background="#0099CC"
05      android:layout_height="fill_parent" >
06  
07      <TextView
08          android:id="@+id/textView1"
09          android:layout_width="wrap_content"
10          android:layout_height="wrap_content"
11          android:layout_centerHorizontal="true"
12          android:layout_centerVertical="true"
13          android:text="Child Activity"
14          android:textSize="35sp"
15          android:textColor="#ffffff" />
16  
17  </RelativeLayout>

manifest.xml

01  <?xml version="1.0" encoding="utf-8"?>
02  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
03      package="com.manish.tabdemo"
04      android:versionCode="1"
05      android:versionName="1.0" >
06  
07      <uses-sdk
08          android:minSdkVersion="8"
09          android:targetSdkVersion="16" />
10  
11      <application
12          android:allowBackup="true"
13          android:icon="@drawable/ic_launcher"
14          android:label="@string/app_name"
15          android:theme="@style/AppTheme" >
16          <activity
17              android:name="com.manish.tabdemo.TabHostActivity"
18              android:label="@string/app_name" >
19              <intent-filter>
20                  <action android:name="android.intent.action.MAIN" />
21  
22                  <category android:name="android.intent.category.LAUNCHER" />
23              </intent-filter>
24          </activity>
25          <activity android:name="com.manish.tabdemo.HomeActivity" />
26          <activity android:name="com.manish.tabdemo.AboutActivity" />
27          <activity android:name="com.manish.tabdemo.ContactActivity" />
28          <activity android:name="com.manish.tabdemo.ChildActivity" />
29          <activity android:name="com.manish.tabdemo.AboutGroupActivity" />
30          <activity android:name="com.manish.tabdemo.ContactGroupActivity" />
31          <activity android:name="com.manish.tabdemo.HomeGroupActivity" />
32      </application>
33  
34  </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 PAGE TOP

http://www.javacodegeeks.com/2013/04/android-child-group-activity.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.