Hello coders, in this article i will show you simplest way with source code to make Intro Slider for your android app using ViewPager and Fragments in android studio. I will also share real code so you can copy and paste and adjust layout of fragments and make your own Intro Slider for your app.

You can see me coding the intro slider on YouTube tutorial :

How to make adapter for ViewPager:

For your ViewPager you have build adapter that will functionality of sliding between fragments. For this we will user FragmentStatePagerAdapter from class androidx.fragment.app.FragmentStatePagerAdapter. When your adapter extends FragmentStatePagerAdapter you need to implements its two methods:

  • Fragment getItem(int position) : this method will take your fragments and return layout from sliding to your ViewPager.
  • int getCount() : Counter of item / Fragments that passed to adapter.

Source Code for Slider :

SliderAdapter.java

package com.raj.introsliderexample;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

public class SliderAdapter extends FragmentStatePagerAdapter {
    public SliderAdapter(@NonNull FragmentManager fm) {
        super(fm);
    }
    public SliderAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, behavior);
    }
    @NonNull
    @Override
    public Fragment getItem(int position) {
        //add your fragments here
        switch (position) {
            case 0:
                return new ScreenOne();

            case 1:
                return new ScreenTwo();

            case 2:
                return new ScreenThree();

            default:
                return new ScreenOne();
        }
    }
    @Override
    public int getCount() {
        //count of your screen
        return 3;
    }
}

MainActivity.java

package com.raj.introsliderexample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    ViewPager viewPager;
    TextView tv_page_counter, tv_next;

    int total = 3 , count = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //find view by ids
        viewPager = findViewById(R.id.viewpager);
        tv_page_counter = findViewById(R.id.tv_page_counter);
        tv_next = findViewById(R.id.tv_next);

        tv_page_counter.setText( count + " / " + total);

        //hide title
        getSupportActionBar().hide();

        //set adapter
        viewPager.setAdapter(new SliderAdapter(getSupportFragmentManager()));

        //change counter on scroll
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                count = 0;
                count = position + 1;
                tv_page_counter.setText( count + " / " + total);

                tv_next.setText("Next");
                if (count == total){
                    tv_next.setText("Continue");
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //on last screen change next button to finish / continue
    }
}

I hope this article will help you to code Intro Slider for your app in android studio. Thanks for visiting. Subscribe my channel to my channel for more coding and life style stuff.

Enjoy coding 🙂


1 Comment

me · October 27, 2019 at 8:50 pm

nice

Leave a Reply

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