Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add tabhost in fragments

i am trying to add tabhost inside a fragment but no matter what i try i am not able to insert it. I might be missing some fundamentals here.Here code of my class TabFragment. Which returns a view.

public class TabFragment extends Fragment{  

    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
    }
    private TabHost mTabHost;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {
          View view = inflater.inflate(R.layout.tabmain, container, false);
          mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
          mTabHost.setup();//very important to call this
          TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content");
          tab.setIndicator("my tab content");
          mTabHost.addTab(tab);
      return view;
    }
}
like image 689
sohil Avatar asked Mar 02 '12 06:03

sohil


People also ask

Which are the two child of the TabHost>?*?

TabHost consists of two children of which one is FrameLayout (which is used to show the contents of the activity) and another one is TabWidget. (It is used to choose the tab which the user wants to open).

What is TabHost?

In Android, TabHost is a Container for tabbed window view. This object holds two children one is set of tab labels that the user clicks to select a specific tab and other is a FrameLayout object that displays the content of that page.

What is FragmentContainerView?

FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout , so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.


1 Answers

With API level 17, this is now possible:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

// This class is the 3rd fragment in my ViewPager, 
// to which I wanted to add 2 tabs....
public class TabHostParentFragment extends Fragment {
private FragmentTabHost mTabHost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);

Bundle arg1 = new Bundle();
arg1.putInt("Arg for Frag1", 1);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"),
    MyNestedFragment1.class, arg1);

Bundle arg2 = new Bundle();
arg2.putInt("Arg for Frag2", 2);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"),
    MyNestedFragment2.class, arg2);

return mTabHost;
}

@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}

Make sure you update your android-support-v4.jar file, as it didnt auto update for me when I downloaded through the SDK manager. This prevents the getChildFragmentManger() function from being defined.

like image 78
jamis0n Avatar answered Oct 19 '22 05:10

jamis0n