I'm having a Fragment
from a mother activity and I want inside it to display two tabs using FragmentTabHost
. But I would like the tabs to be located at the bottom of the fragment not at the top, as it is the default.
MainFragment.java:
public class MainFragment extends Fragment {
private FragmentTabHost tabHost;
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//tabHost = new FragmentTabHost(getActivity());
rootView = inflater.inflate(R.layout.fragment_main, container, false);
tabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);
tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);;
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1"), ConnectFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2"), ManageFragment.class, null);
tabHost.setCurrentTab(0);
return tabHost;
}
}
The layout which I am using is pretty basic and when I run this version everything works but the tabs are displayed at the top of the fragment.
fragment_main.xml:
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
When I am running a slightly different version which is supposed to put the tabs at the bottom of the fragment-according to other threads of this site- i get an error.
fragment_main_v2.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Thrown error:
android.view.InflateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I can imagine that something is going wrong with the
android.view.InflateException: The specified child already has a parent. You must call removeView() on the child's parent first.