helle everyone. please Iwant to know how to build tabhost in a fragment. I want to have the following hierarchy: naviagation drawer -> fragment and in the fragment I load the tabhost. if anyone can help me I waiting please
Asked
Active
Viewed 1,075 times
0
-
i have post some code with example try it. – Vishal Thakkar Aug 10 '16 at 05:50
-
put your efforts.where is your code? – Vishal Thakkar Aug 11 '16 at 06:43
-
Possible duplicate of [how to add tabhost in fragments](http://stackoverflow.com/questions/9529267/how-to-add-tabhost-in-fragments) – Vishal Thakkar Aug 11 '16 at 06:58
-
good question ,I upvoted you – Umar Ata Aug 20 '16 at 12:52
2 Answers
2
you can do like this.
You should use TabLayout instead of TabHost.
Make Xml for main fragment which content Tabs.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Then Make Code like below:
public class TabsFragment extends Fragment {
View view;
PagerAdapter adapter;
private void setupViewPager(ViewPager viewPager) {
///Here we have to pass ChildFragmentManager instead of FragmentManager.
adapter = new PagerAdapter(getChildFragmentManager());
adapter.addFragment(new Fragment1(), "Fragment1");
adapter.addFragment(new Fragment2(), "Fragment2");
viewPager.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.f_tabslayout, container, false);
super.onCreate(savedInstanceState);
final ViewPager viewPager = (ViewPager)view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout)view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
static class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
}

Vishal Thakkar
- 2,117
- 2
- 16
- 33
-
please the "ButterKnife" make error in the onCreateView how can I fix it ? – Arsene Le Virtu-ose Djeukam Aug 11 '16 at 06:16
-
@ArseneLeVirtu-oseDjeukam i have Updated my code . you have to just replace this to getActivity(); – Vishal Thakkar Aug 11 '16 at 06:24
-
-
-
its does this in addition to the old error Error:(26, 16) error: method addFragment in class Adapter cannot be applied to given types; required: Fragment,String found: Fragment1,String reason: actual argument Fragment1 cannot be converted to Fragment by method invocation conversion – Arsene Le Virtu-ose Djeukam Aug 11 '16 at 06:38
-
and the old was: Error:(46, 9) error: cannot find symbol variable ButterKnife – Arsene Le Virtu-ose Djeukam Aug 11 '16 at 06:39
-
have you create Fragment class for each tabs which you want to show? – Vishal Thakkar Aug 11 '16 at 06:41
-
-
-
when you are using Fragment Then there is two lib available you have to use V4 lib fragment – Vishal Thakkar Aug 11 '16 at 06:56
-
import android.app.Fragment; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; – Arsene Le Virtu-ose Djeukam Aug 11 '16 at 06:58
-
this site is not for teach you . its for help to solve query you are beginner you should first go through tutorials. – Vishal Thakkar Aug 11 '16 at 07:00
-
remove import android.app.Fragment; you get error on Fragment then click on it and press alt+enter you get two package in that select v4 one – Vishal Thakkar Aug 11 '16 at 07:00
-
ok I have done it thx but I got the same error with the "ButterKnife" Error:(35, 9) error: cannot find symbol variable ButterKnife – Arsene Le Virtu-ose Djeukam Aug 11 '16 at 07:03
-
-
-
when I add a have this error Error:(37, 20) error: cannot find symbol method bind(FragmentActivity,View) and this Error:(48, 28) error: cannot find symbol variable getActivity – Arsene Le Virtu-ose Djeukam Aug 11 '16 at 07:16
-
please the different fregment are empty yet their different xml file is well been designer. How can I make it visible please? – Arsene Le Virtu-ose Djeukam Aug 12 '16 at 08:50
0
I am assuming that you have implemented Navigation Drawer, and having trouble in calling tabhost in fragment.
Try the following steps.
- Create a parent fragment to hold the tab, in this fragment add as many tabs as you want to add. (In this example I am showing two tabs).
- Create the fragments that you have added in the tabs. (Fragment1, Fragment2)
ParentFragment.java
public class ParentView 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.fragment_parent_view);
mTabHost.getTabWidget().setBackgroundColor(getResources().getColor(R.color.color_primary_dark)); // if you want to set color of tab
Bundle arg1 = new Bundle();
arg1.putString("aim", "passingvalues1");
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("NAME1"),
Fragment1.class, arg1);
Bundle arg2 = new Bundle();
arg2.putString("aim", "passingvalues2");
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Name2"),
Fragment2.class, arg2);
return mTabHost;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
fragment_parent_view.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xx.xx.xx.ParentFragment">
</FrameLayout>
Fragment1.java
public class Fragment1 extends Fragment
{
private LinearLayout llLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
llLayout = (LinearLayout)inflater.inflate(R.layout.fragment1, container, false);
return llLayout;
}
}
Fragment2.java
public class Fragment2 extends Fragment
{
private LinearLayout llLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
llLayout = (LinearLayout)inflater.inflate(R.layout.fragment2, container, false);
return llLayout;
}
}

Rishabh Lashkari
- 638
- 1
- 8
- 22
-
I have try it but I have a BURG "unfortunedly stopped" when I call the fragment – Arsene Le Virtu-ose Djeukam Aug 10 '16 at 11:39
-
is there a specific use to call the fragment in this case? – Arsene Le Virtu-ose Djeukam Aug 10 '16 at 12:17