<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adeyemo Adekunle Ayobami</title>
    <description>The latest articles on DEV Community by Adeyemo Adekunle Ayobami (@kunlexze).</description>
    <link>https://dev.to/kunlexze</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1414479%2F4aab1990-4c69-4187-9459-9f22eb8895f1.jpg</url>
      <title>DEV Community: Adeyemo Adekunle Ayobami</title>
      <link>https://dev.to/kunlexze</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kunlexze"/>
    <language>en</language>
    <item>
      <title>Auto Image Slider in Android | Kotlin: A Step-by-Step Guide</title>
      <dc:creator>Adeyemo Adekunle Ayobami</dc:creator>
      <pubDate>Thu, 11 Apr 2024 07:52:54 +0000</pubDate>
      <link>https://dev.to/kunlexze/auto-image-slider-in-android-kotlin-a-step-by-step-guide-4i74</link>
      <guid>https://dev.to/kunlexze/auto-image-slider-in-android-kotlin-a-step-by-step-guide-4i74</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmgj4c4gi8qsqwrsl46r2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmgj4c4gi8qsqwrsl46r2.png" alt="Auto Image Slider in Android | Kotlin: A Step-by-Step Guide"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently, I added an automatic image slider to my Kotlin app.&lt;/p&gt;

&lt;p&gt;In this post, I'll guide you through the step-by-step process of implementing this feature.&lt;/p&gt;

&lt;p&gt;An image slider is a view that showcases multiple images one at a time. Users can control the slider manually by swiping or using dedicated action buttons to navigate to the next or previous image.&lt;/p&gt;

&lt;p&gt;Let's dive into how you can bring this dynamic image experience to your Kotlin app!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add the needed dependencies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add &lt;code&gt;autoimageslider&lt;/code&gt; and &lt;code&gt;glide&lt;/code&gt; to your &lt;code&gt;build.gradle&lt;/code&gt; (Module) level file in the dependencies section and sync the project with &lt;code&gt;gradle&lt;/code&gt; files.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

implementation 'com.github.smarteist:autoimageslider:1.4.0'
implementation “com.github.bumptech.glide:glide:4.11.0”


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;autoimageslider&lt;/code&gt; library will be used for the image slider and the &lt;code&gt;glide&lt;/code&gt; library will be used to display remote images.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add the SliderView to your main &lt;code&gt;xml&lt;/code&gt; file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open your &lt;code&gt;Activity&lt;/code&gt; or &lt;code&gt;Fragment&lt;/code&gt; &lt;code&gt;xml&lt;/code&gt; file and add the SliderView as shown below.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;androidx.cardview.widget.CardView
    app:cardCornerRadius="6dp"
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"&amp;gt;

    &amp;lt;com.smarteist.autoimageslider.SliderView
        android:id="@+id/imageSlider"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:sliderAnimationDuration="600"
        app:sliderAutoCycleDirection="back_and_forth"
        app:sliderAutoCycleEnabled="true"
        app:sliderIndicatorAnimationDuration="600"
        app:sliderIndicatorGravity="center_horizontal|bottom"
        app:sliderIndicatorMargin="15dp"
        app:sliderIndicatorOrientation="horizontal"
        app:sliderIndicatorPadding="3dp"
        app:sliderIndicatorRadius="2dp"
        app:sliderIndicatorSelectedColor="#5A5A5A"
        app:sliderIndicatorUnselectedColor="#FFF"
        app:sliderScrollTimeInSec="1"
        app:sliderStartAutoCycle="true" /&amp;gt;
&amp;lt;/androidx.cardview.widget.CardView&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The SliderView is wrapped inside a CardView to give the images a rounded corner and make them look better.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Likely error message&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After adding the SliderView above to your &lt;code&gt;xml&lt;/code&gt; file, You could come across any of the following errors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Class referenced in the layout file, &lt;code&gt;com.smarteist.autoimageslider.SliderView&lt;/code&gt;, was not found in the project or the libraries&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cannot resolve class &lt;code&gt;com.smarteist.autoimageslider.SliderView&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution to the error message&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add the code below to your &lt;code&gt;settings.gradle&lt;/code&gt; file&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

dependencyResolutionManagement {
 repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
     repositories {
         //...
         jcenter()
         maven { url 'https://jitpack.io' }
     }
 }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Sync the project with &lt;code&gt;gradle&lt;/code&gt; files and the error message will disappear.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a data class&lt;/strong&gt;&lt;br&gt;
Create a data class named &lt;code&gt;BannerSliderItem&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This data class will have a field named &lt;code&gt;imageUrl&lt;/code&gt; that will hold each slider image url.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class BannerSliderItem (val imageUrl: String)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Create an &lt;code&gt;xml&lt;/code&gt; file for each slider item&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Right-click on your &lt;code&gt;res/layout&lt;/code&gt; folder, and click &lt;code&gt;New&lt;/code&gt; -&amp;gt; &lt;code&gt;Layout Resource File&lt;/code&gt;. Name the file banner_slider_item.xml&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"&amp;gt;
    &amp;lt;ImageView
        android:id="@+id/banner_slider_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:scaleType="fitXY"
        /&amp;gt;

&amp;lt;/RelativeLayout&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Paste the code above inside the &lt;code&gt;xml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a SliderViewAdapter similar to a RecyclerViewAdapter&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class BannerSliderAdapter(sliders: ArrayList&amp;lt;BannerSliderItem&amp;gt;) :
    SliderViewAdapter&amp;lt;BannerSliderAdapter.SliderViewHolder&amp;gt;() {

    var sliderList: ArrayList&amp;lt;BannerSliderItem&amp;gt; = sliders

    override fun getCount(): Int {
        return sliderList.size
    }
    override fun onCreateViewHolder(parent: ViewGroup?): BannerSliderAdapter.SliderViewHolder {
        val inflate: View =
            LayoutInflater.from(parent!!.context).inflate(R.layout.banner_slider_item, null)
        return SliderViewHolder(inflate)
    }

    override fun onBindViewHolder(viewHolder: BannerSliderAdapter.SliderViewHolder?, position: Int) {
        if (viewHolder != null) {
            Glide.with(viewHolder.itemView).load(sliderList.get(position).imageUrl,).fitCenter()
                .into(viewHolder.imageView)
        }
    }

    class SliderViewHolder(itemView: View?) : SliderViewAdapter.ViewHolder(itemView) {
        var imageView: ImageView = itemView!!.findViewById(R.id.banner_slider_view)
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Declare variables&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Declare a variable for the BannerSliderAdapter&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

private lateinit var bannerSliderAdapter: BannerSliderAdapter


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Declare an array list to store the BannerSliderItem items&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

private lateinit var sliderList: ArrayList&amp;lt;BannerSliderItem&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Initialize declared variables&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Initialize the bannerSliderAdapter and sliderList inside the &lt;code&gt;onCreateView&lt;/code&gt; method if you are using a &lt;code&gt;Fragment&lt;/code&gt; or &lt;code&gt;onCreate&lt;/code&gt; method if you're using an &lt;code&gt;Activity&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// Banner Slider Adapter
sliderList = arrayListOf(
    BannerSliderItem("https://images.pexels.com/photos/1639562/pexels-photo-1639562.jpeg"),
    BannerSliderItem("https://images.pexels.com/photos/2983101/pexels-photo-2983101.jpeg"),
    BannerSliderItem("https://images.pexels.com/photos/2271107/pexels-photo-2271107.jpeg")
);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Create an Instance of the BannerSliderAdapter, pass the slider list to the constructor, and assign it to &lt;code&gt;bannerSliderAdapter&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

bannerSliderAdapter = BannerSliderAdapter(sliderList)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Find the reference to the SliderView inside the main layout using its ID and assign it to &lt;code&gt;bannerSliderView&lt;/code&gt;. This reference can be found in two ways.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using findViewById&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

val bannerSliderView: SliderView = findViewById(R.id.bannerSlider);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Using Data Binding&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

val bannerSliderView: SliderView = binding.bannerSlider


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Attach the bannerSliderAdapter instance to the SliderView&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

bannerSliderView.setSliderAdapter(bannerSliderAdapter);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Style the slider&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

//IndicatorAnimationType. :WORM or THIN_WORM or COLOR or DROP //or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP.
bannerSliderView.setIndicatorAnimation(IndicatorAnimationType.WORM);

//Set the SliderTransformAnimation
bannerSliderView.setSliderTransformAnimation(SliderAnimations. WORM);

//Set the autoCycleDirection
bannerSliderView.autoCycleDirection = SliderView.AUTO_CYCLE_DIRECTION_RIGHT;

//Set the indicator selected and unselected color
bannerSliderView.indicatorSelectedColor = Color.WHITE;
bannerSliderView.indicatorUnselectedColor = Color.GRAY;

//Set the slider scroll delay in seconds
bannerSliderView.scrollTimeInSec = 4; 

//Start the auto-cycle
bannerSliderView.startAutoCycle();


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Outcome&lt;/strong&gt;&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://drive.google.com/file/d/1c0g9Q2mQRBLwcDz6qAT-8OETa8mfDCT3/preview" rel="noopener noreferrer" class="c-link"&gt;
          Screen Recording 2024-04-10 at 08.49.11.mov - Google Drive
        &lt;/a&gt;
      &lt;/h2&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fssl.gstatic.com%2Fimages%2Fbranding%2Fproduct%2F1x%2Fdrive_2020q4_32dp.png"&gt;
        drive.google.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, implementing an automatic image slider in your Kotlin app can greatly enhance the visual experience for users. By following the step-by-step guide outlined in this post, you can easily integrate this feature using libraries like autoimageslider and glide. The dynamic nature of the image slider adds a touch of interactivity and engagement to your app. Don't hesitate to experiment with different styles and functionalities to create a unique and captivating image slider for your app users to enjoy!&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>mobile</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
