Android ViewPager padding/margin between page fragments

AndroidAndroid FragmentsAndroid Viewpager

Android Problem Overview


Android Market/Google Music seem to be able to have a gap of some sort between the different fragments that are contained in the ViewPager.

Any idea how this is done? Adding margin/padding to the actual fragment view doesn't work, because the view still needs to occupy the entire width of the screen. The 'gap' is only visible when swiping the ViewPager.

Android Solutions


Solution 1 - Android

In Support Package, r4 you can use these methods:

setPageMargin(int marginPixels)
setPageMarginDrawable(Drawable)
setPageMarginDrawable(int)

in ViewPager class.

I don't know why this doesn't have equivalent in XML :-(

If you need use dip unit you can use this method:

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

Solution 2 - Android

Hoping this might help someone with a ViewPager and page margins.

I wanted to have a ViewPager is a CardView and had issues with the padding and the margin between the viewpager pages. The setPageMargin did not work for me at all. I had to use the following:

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

This allow the pages to be very close together.

Example of the viewpager with a small margin between the pages.

Solution 3 - Android

  viewPager.setPageMargin(dpToPx(10)); 
  // replace 10 with the value you want for margin in dp. 


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

Solution 4 - Android

Try This .

    ViewPager viewPager = (ViewPager) findViewById(R.id.h_view_pager);
    viewPager.setClipToPadding(false);
    viewPager.setPadding(60, 0, 60, 0);
    viewPager.setPageMargin(20);
    ImagePagerAdapter adapter = new ImagePagerAdapter();
    viewPager.setOffscreenPageLimit(adapter.getCount());
    viewPager.setAdapter(adapter);

Solution 5 - Android

pager.setClipToPadding(false); pager.setPadding(left,0,right,0); If you need space between two pages in the viewpager then add pager.setPageMargin(int);

Solution 6 - Android

Try this

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

Solution 7 - Android

here is a sample solution :

implement the ViewPager.OnPageChangeListener in your activity or your fragment where you use the ViewPager

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

        for (int i=0 ; i < tabStripChildCount ; i++){
            View view = mTabStrip.getChildAt(i);

            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
            p.setMargins(0, 0, 50, 0);
            view.requestLayout();
        }
    }

mTabStrip is the table layout in your activity or fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

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

    mViewPager = (ViewPager) view.findViewById(R.id.viewPager); 
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mTabStrip = (SlidingTabLayout) view.findViewById(R.id.tabs);

    mTabStrip.setViewPager(mViewPager);
    return view;
}

finally you will have a margin between your title fragments

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpsychotikView Question on Stackoverflow
Solution 1 - AndroidATomView Answer on Stackoverflow
Solution 2 - AndroidRay HunterView Answer on Stackoverflow
Solution 3 - AndroidAnirudhView Answer on Stackoverflow
Solution 4 - AndroidRambabuView Answer on Stackoverflow
Solution 5 - AndroidRAHULRSANNIDHIView Answer on Stackoverflow
Solution 6 - AndroidAnkit AmanView Answer on Stackoverflow
Solution 7 - AndroidChakib TemalView Answer on Stackoverflow