8

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;
    }
}
onof
  • 17,167
  • 7
  • 49
  • 85
sohil
  • 223
  • 2
  • 4
  • 13
  • 1
    http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/ this link might be helpfull – Triode Mar 02 '12 at 06:58
  • no it doesnt. they are using fragments for making tabs and thats not my requirememnt plus one of my fragment has to b static. i jus need to insert the tabhost somehow – sohil Mar 03 '12 at 04:04
  • @sohil did you get solved..i stuck in same..can you help..check here http://stackoverflow.com/questions/28106944/how-to-add-tabhost-with-navigation-drawer?noredirect=1#comment44592501_28106944? –  Jan 23 '15 at 13:37
  • @Johnson I solved it but it was long time back i don't quite remember how i managed it. sorry – sohil Feb 14 '15 at 17:06

1 Answers1

18

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.

jamis0n
  • 3,610
  • 8
  • 34
  • 50
  • This also works for me using the latest android-support-v4 library. – kdroider Aug 07 '13 at 06:14
  • is there a way to do this without using the support.vx libraries? – lilott8 Sep 13 '14 at 00:51
  • @jamison check here..canyou help http://stackoverflow.com/questions/28106944/how-to-add-tabhost-with-navigation-drawer?noredirect=1#comment44592501_28106944 –  Jan 23 '15 at 13:39
  • thank you so much :) can u tell me how to customize the tabs ?i want to change the underline color,make the text lower case,and change color. – Nevaeh Mar 25 '15 at 08:56