How to Load an Image URL to Carousel View Using Picasso in Kotlin

Introduction

 
Android is an open-source operating system based on Linux with a Java programming interface for mobile devices such as a smartphones and tablets. With over 85% market share worldwide, Android Operating System dominates the mobile platform market. Today, I will show you how to load an image URL to Carousel view, like in an e-commerce app, using Picasso In Kotlin.
 
Prerequisites
 
Follow these steps to load the image URL to Imageview using Glide In Kotlin. I have included the source code in the attachment.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
How To Load The ImageURL To carousel View Like E-commerce App Using Picasso In Kotlin
 
Step 2
 
Now, add an activity and click the "Next" button.
 
How To Load The ImageURL To carousel View Like E-commerce App Using Picasso In Kotlin
 
Step 3
 
You can choose your application name and choose where your project is to be stored. Choose Kotlin language for coding the project. Now, select the version of Android and select the target Android devices, then click the "Finish" button. 
 
How To Load The ImageURL To carousel View Like E-commerce App Using Picasso In Kotlin
 
Step 4
 
Go to the build.grade file. Add third party dependencies for carouselview and Picasso.
 
How To Load The ImageURL To carousel View Like E-commerce App Using Picasso In Kotlin 
 
The third party dependencies are given below:
  1. implementation"com.synnapps:carouselview:0.1.5"  
  2. implementation 'com.squareup.picasso:picasso:2.71828'  
Step 5
 
Go to the manifest file and add the dependency for an internet connection.
 
How To Load The ImageURL To carousel View Like E-commerce App Using Picasso In Kotlin
 
The manifest code is given below.
  1. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
  2. <uses-permission android:name="android.permission.INTERNET" />  
Step 6
 
Go to the activity_main.xml. This XML file contains the design code for your Android app.
 
How To Load The ImageURL To carousel View Like E-commerce App Using Picasso In Kotlin
 
The XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.    xmlns:tools="http://schemas.android.com/tools"  
  5.    android:layout_width="match_parent"  
  6.    android:layout_height="match_parent"  
  7.    tools:context=".MainActivity">  
  8. <com.synnapps.carouselview.CarouselView  
  9.    android:id="@+id/carouselView"  
  10.    android:layout_width="match_parent"  
  11.    android:layout_height="200dp"  
  12.    app:fillColor="#FFFFFFFF"  
  13.    app:pageColor="#00000000"  
  14.    app:radius="6dp"  
  15.    app:slideInterval="3000"  
  16.    app:strokeColor="#FF777777"  
  17.    app:strokeWidth="1dp"  
  18.    app:layout_constraintBottom_toBottomOf="parent"  
  19.    app:layout_constraintLeft_toLeftOf="parent"  
  20.    app:layout_constraintRight_toRightOf="parent"  
  21.    app:layout_constraintTop_toTopOf="parent" />  
  22. </androidx.constraintlayout.widget.ConstraintLayout>  
Step 7
 
Go to Main Activity.kt. This Kotlin program is the back-end language for your app.
 
How To Load The ImageURL To carousel View Like E-commerce App Using Picasso In Kotlin
 
The Kotlin code is given below.
  1. package com.example.carouselview  
  2. import androidx.appcompat.app.AppCompatActivity  
  3. import android.os.Bundle  
  4. import android.widget.ImageView  
  5. import com.squareup.picasso.Picasso  
  6. import com.synnapps.carouselview.CarouselView  
  7. import com.synnapps.carouselview.ImageListener  
  8. class MainActivity : AppCompatActivity() {  
  9.    var sampleImages = arrayOf(  
  10.    "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg",  
  11.    "https://raw.githubusercontent.com/sayyam/carouselview/master/sample/src/main/res/drawable/image_1.jpg",  
  12.    "https://raw.githubusercontent.com/sayyam/carouselview/master/sample/src/main/res/drawable/image_2.jpg"  
  13. )  
  14. override fun onCreate(savedInstanceState: Bundle?) {  
  15.    super.onCreate(savedInstanceState)  
  16.    setContentView(R.layout.activity_main)  
  17.    val carouselView = findViewById(R.id.carouselView) as CarouselView;  
  18.    carouselView.setPageCount(sampleImages.size);  
  19.    carouselView.setImageListener(imageListener);  
  20. }  
  21. var imageListener: ImageListener = object : ImageListener {  
  22.    override fun setImageForPosition(position: Int, imageView: ImageView) {  
  23.       // You can use Glide or Picasso here  
  24.       Picasso.get().load(sampleImages[position]).into(imageView)  
  25.       }  
  26.    }  
  27. }  
Step 8
 
click the "Run" button, or press Shift+F10 to finally run the project. Choose the "virtual machine" option and click OK.
 

Conclusion

 
How To Load The ImageURL To carousel View Like E-commerce App Using Picasso In Kotlin
How To Load The ImageURL To carousel View Like E-commerce App Using Picasso In Kotlin 
How To Load The ImageURL To carousel View Like E-commerce App Using Picasso In Kotlin 


Similar Articles