Wednesday, October 20, 2010

Android : How to have multiple activities under a single tab of TabActivity

Yes, its possible.
1. Lets first have our main activity as
public class multiple extends TabActivity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();  // The activity TabHost
TabHost.TabSpec spec;  // Resusable TabSpec for each tab
Intent intent;  // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Activity1.class);
spec = tabHost.newTabSpec("Activity1").setIndicator(new CustomTabView(this,
R.drawable.options_selected, "Activity1")) .setContent(intent);
tabHost.addTab(spec); // Do the same for the other tabs
intent = new Intent().setClass(this, Activity2.class);
spec = tabHost.newTabSpec("Activity2").setIndicator(new CustomTabView(this, R.drawable.options_selected, "Activity2")) .setContent(intent);
tabHost.addTab(spec);
}
 
}
 
 
2. Here we create 2 tabs. Activity1  Activity2
Activity1 tab launches Activity1
Activity2 tab launches Activity2
 
Now tricky part is how to launch Activity3 from Activity1 so that Activity3 also shows the tabs.
 
3. To achieve this we extend Activity1 to be derived from ActivityGroup. Clicking on a button in activity1 will launch Activity3 as subactivity of Activity1.
 
public class Activity1 extends ActivityGroup {
protected static LocalActivityManager mLocalActivityManager;
Button launchButton;
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
launchButton = (Button)findViewById(R.id.Button01);
launchButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent activity3Intent = new Intent(v.getContext(), Activity3.class);
StringBuffer urlString = new StringBuffer();
//Activity1 parentActivity = (Activity1)getParent();
replaceContentView("activity3", activity3Intent);
}
});
 
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}        }
 
4. We can even launch another activity, Activity4 from Activity3 and still have the tabs at the bottom.
public class Activity3 extends Activity {
Button launchButton;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity3);
launchButton = (Button)findViewById(R.id.Button01);
launchButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {               Intent activity4Intent = new Intent(v.getContext(), Activity4.class);
StringBuffer urlString = new StringBuffer();
Activity1 parentActivity = (Activity1)getParent();
parentActivity.replaceContentView("activity4", activity4Intent);
} }); }}
 
Enjoy!!

Your Ad Here