0 Replies - 2067 Views - Last Post: 07 October 2012 - 10:56 PM

#1 Larsonator  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 77
  • Joined: 13-August 10

TabsHost and Fragments - Button to add new Tab

Posted 07 October 2012 - 10:56 PM

So I have followed a certain tutorial on managing TabHost and Fragments, and Creating a Fragment manually with code is fine. How ever, i want the first tab to be a kind of menu tab, where i can either create a new tab, or continue with calculation.

But the way that the tabs are created are through an Activity Class, so essentially, to add a new tab, i have to call a function in another activity.

Il Post some code and it might make more sense.
import java.util.HashMap;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;

public class CompareSimulation extends FragmentActivity implements TabHost.OnTabChangeListener {

	public TabHost mTabHost;
	private HashMap mapTabInfo = new HashMap();
	private TabInfo mLastTab = null;

	public class TabInfo {
		 private String tag;
         private Class clss;
         private Bundle args;
         private Fragment fragment;
         TabInfo(String tag, Class clazz, Bundle args) {
        	 this.tag = tag;
        	 this.clss = clazz;
        	 this.args = args;


	class TabFactory implements TabContentFactory {

		private final Context mContext;

	     * @param context
	    public TabFactory(Context context) {
	        mContext = context;

	    /** (non-Javadoc)
	     * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
	    public View createTabContent(String tag) {
	        View v = new View(mContext);
	        return v;

	/** (non-Javadoc)
	 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
	protected void onCreate(Bundle savedInstanceState) {
		// Step 1: Inflate layout
		// Step 2: Setup TabHost
		if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state


	/** (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected

	 * Step 2: Setup TabHost
	private void initialiseTabHost(Bundle args) {
		mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        TabInfo tabInfo = null;
        CompareSimulation.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Menu").setIndicator("Menu"), ( tabInfo = new TabInfo("Menu", ComparSimMenu.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        CompareSimulation.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Sim1").setIndicator("Sim 1"), ( tabInfo = new TabInfo("Sim1", Comp_Simulation.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        // Default to first tab

	 * @param activity
	 * @param tabHost
	 * @param tabSpec
	 * @param clss
	 * @param args
	private static void addTab(CompareSimulation activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
		// Attach a Tab view factory to the spec
		tabSpec.setContent(activity.new TabFactory(activity));
        String tag = tabSpec.getTag();

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
        if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();


	/** (non-Javadoc)
	 * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
	public void onTabChanged(String tag) {
		TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag);
		if (mLastTab != newTab) {
			FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(this,
                            newTab.clss.getName(), newTab.args);
                    ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
                } else {

            mLastTab = newTab;


the static function addTab is the function i need to call to create a new tab, but the menuTab has been created using the ComparSimMenu Class
package solar.power.calculator.android;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class ComparSimMenu extends Fragment{

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(container == null)
			return null;
		LinearLayout mLinearLayout = (LinearLayout)inflater.inflate(R.layout.compar_sim_menu, container, false);
		Button newSim = (Button)mLinearLayout.findViewById(R.id.comp_sim_menu_newSim);
/*On this button click i want to make a new tab*/
		newSim.setonclickListener(new View.onclickListener() {
			public void onclick(View v) {
				// TODO Auto-generated method stub
		return mLinearLayout;

Anyone have any idea's?

The Button to create a new tab doesnt have to be in a tab itself, if its easyer, its perfectly find to have it maybe above the tabhost it self, which i have been messing around with, implementing the onclickListener as well, and creating a new tab on an onclick event, but that hasn't been working too much for me either, soo any help will still be appreciated.

This post has been edited by Larsonator: 08 October 2012 - 12:46 AM

Is This A Good Question/Topic? 0
  • +

Page 1 of 1