Android Auto Image Slider with Kotlin
Most e-commerce application uses auto image slider to display ads, and offers within their application. Auto Image Slider is used to display data in the form of slides. In this article, we will take a look at the Implementation of Auto Image Slider in our Android application using Kotlin. A sample video is given below to get an idea about what we are going to do in this article.
Note: If you are looking to implement Auto Image Slider within your android application in Java. Check out the following article: Auto Image Slider in Android in Java
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Add dependency of Slider View in build.gradle file
Navigate to the Gradle scripts and then build.gradle(Module) level. Add below line in build.gradle file in the dependencies section.
// dependency for slider view implementation ‘com.github.smarteist:autoimageslider:1.3.9’ // dependency for loading image from url implementation “com.github.bumptech.glide:glide:4.11.0”
After adding this dependency sync your project to install it.
Step 3: Add internet permission in the AndroidManifest.xml file
Navigate to the app > Manifest to open the Manifest file and add below two lines in the manifest tag.
XML
<!--internet permissions and network state permission--> < uses-permission android:name = "android.permission.INTERNET" /> < uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" /> |
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!--on below line we are creating a new text view--> < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "50dp" android:gravity = "center" android:padding = "8dp" android:text = "Auto Image Slider" android:textAlignment = "center" android:textColor = "@color/purple_200" android:textSize = "20sp" android:textStyle = "bold" /> <!-- slideranimation duration is to set duration for transition between two slides sliderautocycledirection is to set animationbetween transition of your slides sliderindicator enables is used to display the indicators for slider slider indicator gravity is to set gravity for indicator gravity slider indicator margin is to set margin for indicator slider indicator orientation is used to add orientation for slider slider indicator padding is use to add padding to indicator slider indicator selected color is use to specify selected color and slider indicator unselected color is use to specify the color when the slider is unselected slider scroll time in sec is used to specify scrolling time in seconds --> < com.smarteist.autoimageslider.SliderView android:id = "@+id/slider" android:layout_width = "match_parent" android:layout_height = "150dp" android:layout_centerInParent = "true" app:sliderAnimationDuration = "600" app:sliderAutoCycleDirection = "back_and_forth" app:sliderIndicatorAnimationDuration = "600" app:sliderIndicatorEnabled = "true" 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" /> </ RelativeLayout > |
Step 5: Create an XML file for the items of SliderView
Navigate to the app > res > layout > Right-click on it and select New > Layout Resource File and then name your XML file as slider_item.xml. After creating this file add the below code to it.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" > <!--Image we will display is our slider view--> < ImageView android:id = "@+id/myimage" android:layout_width = "400dp" android:layout_height = "300dp" android:layout_centerHorizontal = "true" android:contentDescription = "@string/app_name" /> </ RelativeLayout > |
Step 6: Create an Adapter Class for setting data to each item of our SliderView
Navigate to app > java > your app’s package name and then right-click on it and New > Kotlin class and name your class as SliderAdapter and below code inside that Kotlin class. Below is the code for the SliderAdapter.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import com.bumptech.glide.Glide import com.smarteist.autoimageslider.SliderViewAdapter // on below line we are creating a class for slider // adapter and passing our array list to it. class SliderAdapter(imageUrl: ArrayList<String>) : SliderViewAdapter<SliderAdapter.SliderViewHolder>() { // on below line we are creating a // new array list and initializing it. var sliderList: ArrayList<String> = imageUrl // on below line we are calling get method override fun getCount(): Int { // in this method we are returning // the size of our slider list. return sliderList.size } // on below line we are calling on create view holder method. override fun onCreateViewHolder(parent: ViewGroup?): SliderAdapter.SliderViewHolder { // inside this method we are inflating our layout file for our slider view. val inflate: View = LayoutInflater.from(parent!!.context).inflate(R.layout.slider_item, null ) // on below line we are simply passing // the view to our slider view holder. return SliderViewHolder(inflate) } // on below line we are calling on bind view holder method to set the data to our image view. override fun onBindViewHolder(viewHolder: SliderAdapter.SliderViewHolder?, position: Int) { // on below line we are checking if the view holder is null or not. if (viewHolder != null ) { // if view holder is not null we are simply // loading the image inside our image view using glide library Glide.with(viewHolder.itemView).load(sliderList.get(position)).fitCenter() .into(viewHolder.imageView) } } // on below line we are creating a class for slider view holder. class SliderViewHolder(itemView: View?) : SliderViewAdapter.ViewHolder(itemView) { // on below line we are creating a variable for our // image view and initializing it with image id. var imageView: ImageView = itemView!!.findViewById(R.id.myimage) } } |
Step 7: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.smarteist.autoimageslider.SliderView class MainActivity : AppCompatActivity() { // on below line we are creating a variable // for our array list for storing our images. lateinit var imageUrl: ArrayList<String> // on below line we are creating // a variable for our slider view. lateinit var sliderView: SliderView // on below line we are creating // a variable for our slider adapter. lateinit var sliderAdapter: SliderAdapter override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // on below line we are initializing our slier view. sliderView = findViewById(R.id.slider) // on below line we are initializing // our image url array list. imageUrl = ArrayList() // on below line we are adding data to our image url array list. imageUrl = (imageUrl + "https://practice.geeksforgeeks.org/_next/image?url=https%3A%2F%2Fmedia.geeksforgeeks.org%2Fimg-practice%2Fbanner%2Fdsa-self-paced-thumbnail.png&w=1920&q=75" ) as ArrayList<String> imageUrl = (imageUrl + "https://practice.geeksforgeeks.org/_next/image?url=https%3A%2F%2Fmedia.geeksforgeeks.org%2Fimg-practice%2Fbanner%2Fdata-science-live-thumbnail.png&w=1920&q=75" ) as ArrayList<String> imageUrl = (imageUrl + "https://practice.geeksforgeeks.org/_next/image?url=https%3A%2F%2Fmedia.geeksforgeeks.org%2Fimg-practice%2Fbanner%2Ffull-stack-node-thumbnail.png&w=1920&q=75" ) as ArrayList<String> // on below line we are initializing our // slider adapter and adding our list to it. sliderAdapter = SliderAdapter( imageUrl) // on below line we are setting auto cycle direction // for our slider view from left to right. sliderView.autoCycleDirection = SliderView.LAYOUT_DIRECTION_LTR // on below line we are setting adapter for our slider. sliderView.setSliderAdapter(sliderAdapter) // on below line we are setting scroll time // in seconds for our slider view. sliderView.scrollTimeInSec = 3 // on below line we are setting auto cycle // to true to auto slide our items. sliderView.isAutoCycle = true // on below line we are calling start // auto cycle to start our cycle. sliderView.startAutoCycle() } } |
Now run your application to see the output of it.
Output:
Please Login to comment...