Android studio: how add tabs with ViewPager

Pablito picture Pablito · Oct 8, 2016 · Viewed 7.3k times · Source

I want to add in a fragment a tab with pagerview (scrollable).

    public class MyFragment extends Fragment {
    private FragmentTabHost tabHost;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        tabHost = new FragmentTabHost(getActivity());
        tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
        tabHost.addTab(tabHost.newTabSpec("one").setIndicator("One"), OneFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("two").setIndicator("Two"), TwoFragment.class, null);
        return tabHost;
    }


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

With this layout

<?xml version="1.0" encoding="utf-8"?>
<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">

        <android.support.v4.view.ViewPager
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

I tried several solutions, but do not work. I need to use fragment, not fragmentActivity. The code written up work.

Answer

Khemraj Sharma picture Khemraj Sharma · Nov 16, 2018

Small Code for Tablayout + ViewPager

// find views by id
ViewPager vp= findViewById(R.id.viewpager);
TabLayout tl = findViewById(R.id.tablayout);

// attach tablayout with viewpager
tl.setupWithViewPager(vp);

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

// add your fragments
adapter.addFrag(new SampleFragment(), "Tab1");
adapter.addFrag(new SampleFragment(), "Tab2");
adapter.addFrag(new SampleFragment(), "Tab3");

// set adapter on viewpager
vp.setAdapter(adapter);

XML layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

Note If you are not using AndroidX yet, you need to change following in layout.

  • Change com.google.android.material.tabs.TabLayout to android.support.design.widget.TabLayout
  • Chagne androidx.viewpager.widget.ViewPager to android.support.v4.view.ViewPager

But I'll strongly recommend to migrate to AndroidX, see @this answer to understand why.

And this is common ViewPagerAdapter for all your Viewpager in app.

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

    public void addFrag(Fragment fragment) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add("");
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }
}

If you need to set ViewPager in Fragment, please check @this answer.