How to load images uploaded from internet using RecyclerView in Android

Tiempo de lectura: 2 minutos

Reading time: 2 minutes

Hello everyone, this time I’m going to explain how to load images from a URL into a RecyclerView in Android.

If you don’t know how to create a RecyclerView, I recommend you visit the previous post on How to Create a RecyclerView in Android.

Now, let’s get started.

The first thing is to add an ImageView inside the layout that represents a row of the RecyclerView. To do this, follow these steps:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="100dp">
 
    <RelativeLayout
        android:id="@+id/capaPrincipal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/capaPrincipal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="30dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/imagen"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:padding="10dp"/>
            </LinearLayout>
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

Once added, assign the id “imagen” android:id="@+id/imagen" to reference it from the RecyclerView’s holder as follows:

public class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView imagen;
   
    public int type;
   
    public ViewHolder(View itemView, int type) {
        super(itemView);
       
        imagen = itemView.findViewById(R.id.imagen);
 
        this.type = type;
    }
}

Since this is an image loaded remotely, before adding it to the RecyclerView item, we need to add a library to our project that will handle loading the corresponding image asynchronously directly into the ImageView. For this purpose, we can use two popular libraries: Glide (https://github.com/bumptech/glide) and Picasso (https://square.github.io/picasso/).

In this example, I have used Picasso, but both libraries work similarly.

First, we need to add the library to the app-level build.gradle file under dependencies:

dependencies {
    implementation 'com.squareup.picasso:picasso:2.5.2'
}

Once added, click on “Sync Now” to download the library.

Now we can use the library in our RecyclerView item.

Simply add the following line in the onBindViewHolder method:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {

    String imageUrl = "https://devcodelight.com/wp-content/uploads/2022/02/cropped-cropped-cropped-cropped-light_logo-1.png";
    Picasso.with(context).load(imageUrl).fit().centerCrop().into(holder.imagen);
}

Remember to pass the Context context through the RecyclerView constructor.

And that’s it! This is how easily we canReading time: 2 minutes

Hello everyone, this time I’m going to explain how to load images obtained from a URL into a RecyclerView in Android.

If you don’t know how to create a RecyclerView, I recommend you visit the previous post on How to Create a RecyclerView in Android.

Now, let’s get started.

The first thing you need to do is add an ImageView inside the layout that represents a row of the RecyclerView. Follow these steps:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="100dp">
 
    <RelativeLayout
        android:id="@+id/capaPrincipal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/capaPrincipal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="30dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/imagen"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:padding="10dp"/>
            </LinearLayout>
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

Once added, assign the id “imagen” android:id="@+id/imagen" to reference it from the RecyclerView’s holder as follows:

public class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView imagen;
   
    public int type;
   
    public ViewHolder(View itemView, int type) {
        super(itemView);
       
        imagen = itemView.findViewById(R.id.imagen);
 
        this.type = type;
    }
}

Since this is an image loaded remotely, before adding it to the RecyclerView item, we need to add a library to our project that will handle loading the corresponding image asynchronously directly into the ImageView. For this purpose, we can use two popular libraries: Glide (https://github.com/bumptech/glide) and Picasso (https://square.github.io/picasso/).

In this example, I have used Picasso, but both libraries work similarly.

First, we need to add the library to the app-level build.gradle file under dependencies:

dependencies {
    implementation 'com.squareup.picasso:picasso:2.5.2'
}

Once added, click on “Sync Now” to download the library.

Now we can use the library in our RecyclerView item.

Simply add the following line in the onBindViewHolder method:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {

    String imageUrl = "https://devcodelight.com/wp-content/uploads/2022/02/cropped-cropped-cropped-cropped-light_logo-1.png";
    Picasso.with(context).load(imageUrl).fit().centerCrop().into(holder.imagen);
}

Remember to pass the Context context through the RecyclerView constructor.

And that’s it! This is how easily we can load images into our RecyclerViews.

Leave a Comment