Adding Tab inside Fragment In Android?
Try to do this to handle the Tabs:
public class MainFragment extends Fragment { private FragmentTabHost mTabHost; //Mandatory Constructor public MainFragment() { } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_tabs,container, false); mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent); mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"), FragmentB.class, null); mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"), FragmentC.class, null); mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"), FragmentD.class, null); return rootView; }}
With the layout:
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout></android.support.v4.app.FragmentTabHost>
The MotherActivity to host the MainFragment:
public class MotherActivity extends FragmentActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MainFragment fragmenttab = new MainFragment(); getSupportFragmentManager().beginTransaction() .add(R.id.item_detail_container, fragmenttab).commit(); }
And MotherActivity layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/item_detail_container" android:layout_width="match_parent" android:layout_height="match_parent" />
After this just create the normal fragment B and C, etc Class. The Result will be:
Try adding this line
tabHost.setup(this.getActivity().getLocalActivityManager());
before the line
tabHost.addTab(spec);
or try adding:
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this.getActivity(), false);mLocalActivityManager.dispatchCreate(savedInstanceState);host.setup(mLocalActivityManager);