android – Swipe between fragment with ViewPager and without TabLayout-ThrowExceptions

Exception or error:

I have 3 fragments (totally different from each other) and one activity (MainActivity). What I would like to do is to be able to swipe between them (with finger, not with buttons) with a transition like a TabLayout.

According to what I saw, I can do it using ViewPager. But the problem is that ViewPager uses TabLayout.

There is a way to swipe betweens fragments, using Viewpager, without TabLayout ?

How to solve:

This code will help you


<?xml version="1.0" encoding="utf-8"?>

    app:layout_constraintTop_toTopOf="parent" />

Set up the child Fragments you would like to display. I made 3 child Fragments, calling them ChildFragment1, ChildFragment2, and ChildFragment3. Remember to have them extend Now make layouts for all three of the Fragments. I call them child_fragment_1_layout, child_fragment_2_layout, and child_fragment_3_layout.

public class ChildFragment1 extends Fragment {

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View rootView = inflater.inflate(R.layout.child_fragment_1_layout, container, false);
    Button buttonInFragment1 = rootView.findViewById(;
    buttonInFragment1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Toast.makeText(getContext(), "button in fragment 1", Toast.LENGTH_SHORT).show();

    return rootView;

Make an adapter for the ViewPager. You will have to extend either theFragmentPagerAdapter or the FragmentStatePagerAdapter. For this tutorial we will use the FragmentPagerAdapter . The difference between the two can be found here. After extending the FragmentPagerAdapter, you will need to call super(FragmentManager) in your constructor and implement the methods getItem(position) and getCount(). The getItem(position)method is used to return the fragment at the corresponding position, ordered from left to right. So ChildFragment1 would be at position 0, ChildFragment2 would be at position 1, and ChildFragment3 would be at position 2. The getCount() method is to count how many Fragments there are to display, and in this case, there are 3.

public class ViewPagerAdapter extends FragmentPagerAdapter {

public ViewPagerAdapter(FragmentManager fm) {

public Fragment getItem(int position) {
    switch (position)
        case 0:
            return new ChildFragment1(); //ChildFragment1 at position 0
        case 1:
            return new ChildFragment2(); //ChildFragment2 at position 1
        case 2:
            return new ChildFragment3(); //ChildFragment3 at position 2
    return null; //does not happen

public int getCount() {
    return 3; //three fragments

Now find your ViewPager in MainActivity and call the setAdapter() method and pass in your custom adapter. If you are doing this in another Fragment (Nested Fragments), you will have to pass in getChildFragmentManager() in the argument of your adapter instead. Now your ViewPager is all set

public class MainActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {

    ViewPager viewPager = findViewById(;
    viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));

Leave a Reply

Your email address will not be published. Required fields are marked *