Android ViewPager2: Horizontal Show Partial Preview of Left and Right

Setup Horizontal ViewPage2.

viewPager.apply {    clipToPadding = false   // allow full width shown with padding    clipChildren = false    // allow left/right item is not clipped    offscreenPageLimit = 2  // make sure left/right item is rendered}// increase this offset to show more of left/rightval offsetPx = 30.dpToPx(resources.displayMetrics)viewPager.setPadding(offsetPx, 0, offsetPx, 0)// increase this offset to increase distance between 2 itemsval pageMarginPx = 10.dpToPx(resources.displayMetrics)val marginTransformer = MarginPageTransformer(pageMarginPx)viewPager.setPageTransformer(marginTransformer)

NOTE: Convert Dp to Pixel - dpToPx

NOTE: I am using androidx.viewpager2:viewpager2:1.0.0-beta02

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.