Android ViewPager padding/margin between page fragments –

Development issue/problem:

Android Market/Google Music seems to be able to have some kind of separation between the different clips recorded in ViewPager.

Do you have any idea how to do this? Adding notches/nicks in the actual sectional view does not work because the view should always cover the full width of the screen. The hole is only visible when the ViewPager is filtered.

How can I solve this problem?

Solution 1:

You can use these methods in the r4 support package:

setPageMargin(int marginPixels)

in the ViewPager class.

I don’t know why there’s no XML equivalent.

If you need diving equipment, you can use this method:

public static int convertDip2Pixels(Context, int dip) {
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics());

Solution 2:

I hope this will help someone with ViewPager and page fields.

I wanted the ViewPager to be a CardView, and I had problems with padding and spaces between the pages in the ViewPager. SetPageMargin didn’t work for me at all. I had to use the following method:

int pagerPadding = 16;
pager.setPadding(pagerPadding, 0, pagerPadding, 0) ;

This allows the pages to be very close to each other.

An example window with a small indentation between the pages.

Solution 3:

Try this.

ViewPager viewPager = (ViewPager) findViewById(;
viewPager.setPadding(60, 0, 60, 0);
adapt ImagePagerAdapter = new ImagePagerAdapter();
viewPager.setAdapter(adapt) ;

Solution 4:

// replace 10 with the margin value in dp.

public int dpToPx(int dp) {
DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics() ;
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)) ;

Solution No 5:

If you need space between two pages in the viewpager, add

Solution No 6:

Try this.

viewPager.setClipToPadding(false) ;
viewPager.setOffscreenPageLimit(2) ;
viewPager.setPadding(20, 0, 20, 0) ;

Solution No 7:

Here is an example of a solution:

Integrate ViewPager.OnPageChangeListener into your business or the snippet where you use ViewPager.

public blank onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int tabStripChildCount = mTabStrip.getChildCount() ;

for (int i=0; i

mTabStrip is the table of your activity or fragment.

General view
onCreate view (LayoutInflater inflater, ViewGroup container, Bundle storedInstanceState) {

mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager()) ;
View View = inflater.inflate(R.layout.patient_activity__fragment_tab_viewer, container, false) ;

mViewPager = (ViewPager) view.findViewById( ;
mViewPager.setAdapter(mSectionsPagerAdapter) ;

mTabStrip = (SlidingTabLayout) view.findViewById( ;

mTabStrip.setViewPager; return type

Finally, leave a margin between your headers.

Good luck!

Related Tags:

viewpager2,viewpager with fragments example in android,android-viewpager skip page,android-viewpager with next and previous visible pages,android-viewpager change page from fragment,android-viewpager multiple pages on screen,viewpager2 padding,viewpager2 page margin,addonpagechangelistener in viewpager 2,viewpager2 offscreenpagelimit,viewpager2 get fragment at position