<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fernando Zanutto on Medium]]></title>
        <description><![CDATA[Stories by Fernando Zanutto on Medium]]></description>
        <link>https://medium.com/@fzanutto?source=rss-5d25f0496b3a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*eWMPhdiYQAC52Omx41k3lA.jpeg</url>
            <title>Stories by Fernando Zanutto on Medium</title>
            <link>https://medium.com/@fzanutto?source=rss-5d25f0496b3a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 13:37:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fzanutto/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Carousel in Android using ViewPager2]]></title>
            <link>https://medium.com/@fzanutto/building-a-carousel-in-android-using-viewpager2-e5b8db58c600?source=rss-5d25f0496b3a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5b8db58c600</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Fernando Zanutto]]></dc:creator>
            <pubDate>Wed, 24 May 2023 15:18:05 GMT</pubDate>
            <atom:updated>2023-05-24T15:30:39.173Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*ugyAgr3z1JE3mVGIOjLBwQ.png" /></figure><p>Recently, I had to implement a Carousel that shows a preview of the items by the side of the current item. I didn’t want to use external libraries, as I thought this should be simple enough, but I also didn’t find any straightforward guide explaining how to do this. So here it is, a simple and fast guide to implement the following behavior:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*zmZcaONxtXt1hUszMvsptQ.gif" /></figure><h3>The basic setup</h3><p>Let’s create a really simple project so we can focus on the specifics of ViewPager2 configuration.</p><p>We’ll have an adapter that will receive a list of colors. These colors will set the background color of our views, which are as simple as the following layout:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e6ebc023be27e956cfdac6981044333/href">https://medium.com/media/9e6ebc023be27e956cfdac6981044333/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2aafb5f7774850ae9e7221ff253c0e6/href">https://medium.com/media/e2aafb5f7774850ae9e7221ff253c0e6/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df76070e0cd5508e03745d02f5b90f2e/href">https://medium.com/media/df76070e0cd5508e03745d02f5b90f2e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fe29a62304cea4da3baac52f5c63418/href">https://medium.com/media/5fe29a62304cea4da3baac52f5c63418/href</a></iframe><p>This should have the following result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*JRCAZlVMSe_eMwZDiui1Ng.gif" /></figure><h3>ViewPager2 configuration</h3><p>To achieve our goal, we now need to do 3 things:</p><h4>1) ViewPager2.setOffscreenPageLimit()</h4><p>We must set the offscreen page limit, so ViewPager2 loads and keeps the side pages on screen, and not only the currently selected page.</p><pre>binding.viewPager2.offscreenPageLimit = 1</pre><h4>2) Setting the ViewPager2 ItemDecoration</h4><p>Let’s say that each page will have <em>32dp</em> of horizontal margin to the parent container, and already define that the visible part of the side items will be <em>24dp. </em>Any number can be used here, as long as the side item visibility is lower than the item horizontal margin, or else they will overlap.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df512ab46575a9dd931d657fbd5ca8bf/href">https://medium.com/media/df512ab46575a9dd931d657fbd5ca8bf/href</a></iframe><p>In MainActivity.kt add the following:</p><pre>val itemDecoration = object: RecyclerView.ItemDecoration() {<br>      override fun getItemOffsets(<br>          outRect: Rect,<br>          view: View,<br>          parent: RecyclerView,<br>          state: RecyclerView.State<br>      ) {<br>          outRect.left = horizontalItemMargin.toInt()<br>          outRect.right = horizontalItemMargin.toInt()<br>      }<br>  }<br>  binding.viewPager2.addItemDecoration(itemDecoration)</pre><p>Right now, this should be the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*2ji0TVZA1SRu9oJF0-CvPA.gif" /></figure><h4>3) And finally, ViewPager2.PageTransformer</h4><p>ViewPager2 has a <em>setPagerTransform</em> method, which accepts the ViewPager2.PageTransformer interface as a parameter. With this PageTransformer we can manipulate the View object of each page, and change properties like translation (x and y), alpha, scale, etc…</p><p>With this, we add the following in MainActivity.kt file:</p><pre>// How much of the side items will be visible<br>val sideItemVisibility = resources.getDimension(R.dimen.side_item_visibility)<br>// Horizontal margin from current item side to the parent container<br>val horizontalItemMargin = resources.getDimension(R.dimen.horizontal_margin)<br><br>val pageTranslationX = horizontalItemMargin + sideItemVisibility<br>val pageTransformer = ViewPager2.PageTransformer { page: View, position: Float -&gt;<br>    page.translationX = -pageTranslationX * position<br>}<br>binding.viewPager2.setPageTransformer(pageTransformer)</pre><p>We are bringing each page a little to the left so it is visible even though we are not currently in that page. The pageTranslationX is the result of adding s<em>ideItemVisibility</em> and <em>horizontalItemMargin</em> because in the previous step we added a decorator to every item, so if we want the side items to show where we want, we must make up for it in this step.</p><p>And, it is done! You should have the expected behavior and now you can customize your views and add a bit more of life to the project!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/279/1*8fUSI2I0u347uPo1UqnFUQ.gif" /></figure><p>The final code is available in my Github. There is a branch called “<em>demo-with-better-layout”</em> that has the code resulting in the first GIF of this article.</p><p><a href="https://github.com/fernandozanutto/CarouselViewPager2-Android-Sample">GitHub - fernandozanutto/CarouselViewPager2-Android-Sample</a></p><p>Feel free to comment or ask questions! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5b8db58c600" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>