

Prerequisite
Take a look at my previous RecyclerView article to implement a simple RecyclerView.
Picasso is a image loading library by Square Inc. It makes image loading easier. It also provides disk caching and makes networking easier. For example if you are using Picasso in a adapter it loads image only once and reuses the cached image for the next time.
Picasso
To learn more about Picasso.
Here I have created a Android Studio project with package com.learn2crack.loadimage also Activity as MainActivity and layout as activity_main.
You can download the complete project as zip or fork from our Github repository.
The dependency for Picasso is
compile 'com.squareup.picasso:picasso:2.5.2'
The other dependencies we require,
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
The complete build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.learn2crack.loadimage"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
}
We need to add Internet permission in our AndroidManifest.xml
Our main layout has only one RecyclerView widget in CoordinatorLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.learn2crack.loadimage.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/card_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
The next layout is for RecylerView list item. It has a TextView and ImageView child widgets to display android version name and android version image.
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<RelativeLayout
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="120dp"
android:layout_height="60dp"
android:id="@+id/img_android"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"/>
<TextView
android:layout_marginTop="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_android"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/img_android" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Now we create AndroidVersion model class to store android version name and android image URL as String. It has getter and setter methods.
package com.learn2crack.loadimage;
public class AndroidVersion {
private String android_version_name;
private String android_image_url;
public String getAndroid_version_name() {
return android_version_name;
}
public void setAndroid_version_name(String android_version_name) {
this.android_version_name = android_version_name;
}
public String getAndroid_image_url() {
return android_image_url;
}
public void setAndroid_image_url(Stringandroid_image_url) {
this.android_image_url = android_image_url;
}
}
We are using the adapter similar to the one we used in our previous projects. We pass AndroidVersion model class as ArrayList and Context in constructor.
Here comes the magic line. We load image from url into ImageView by using the code
Picasso.with(context).load(android_versions.get(i).getAndroid_image_url()).resize(120, 60).into(viewHolder.img_android);
This single line of code simplifies all your complex tasks. The img_android is the ImageView object. The resize() method re sizes the image obtained to specified dimensions and loads to ImageView. This also reduces the memory usage.
package com.learn2crack.loadimage;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<AndroidVersion> android_versions;
private Context context;
public DataAdapter(Context context,ArrayList<AndroidVersion> android_versions) {
this.context = context;
this.android_versions = android_versions;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.tv_android.setText(android_versions.get(i).getAndroid_version_name());
Picasso.with(context).load(android_versions.get(i).getAndroid_image_url()).resize(120, 60).into(viewHolder.img_android);
}
@Override
public int getItemCount() {
return android_versions.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView tv_android;
ImageView img_android;
public ViewHolder(View view) {
super(view);
tv_android = (TextView)view.findViewById(R.id.tv_android);
img_android = (ImageView)view.findViewById(R.id.img_android);
}
}
}
In our Activity the android version names are defined in the array android_version_names. Similarly the image urls are defined in android_image_urls. We create a new AndroidVersion object and use getters and setters to set the version name and image URL. Finally we add all those objects to ArrayList.
Finally a new DataAdapter object is created and set to RecyclerView using setAdapter() method.
package com.learn2crack.loadimage;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private final String android_version_names[] = {
"Donut",
"Eclair",
"Froyo",
"Gingerbread",
"Honeycomb",
"Ice Cream Sandwich",
"Jelly Bean",
"KitKat",
"Lollipop",
"Marshmallow"
};
private final String android_image_urls[] = {
"http://api.learn2crack.com/android/images/donut.png",
"http://api.learn2crack.com/android/images/eclair.png",
"http://api.learn2crack.com/android/images/froyo.png",
"http://api.learn2crack.com/android/images/ginger.png",
"http://api.learn2crack.com/android/images/honey.png",
"http://api.learn2crack.com/android/images/icecream.png",
"http://api.learn2crack.com/android/images/jellybean.png",
"http://api.learn2crack.com/android/images/kitkat.png",
"http://api.learn2crack.com/android/images/lollipop.png",
"http://api.learn2crack.com/android/images/marshmallow.png"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
ArrayList androidVersions = prepareData();
DataAdapter adapter = new DataAdapter(getApplicationContext(),androidVersions);
recyclerView.setAdapter(adapter);
}
private ArrayList prepareData(){
ArrayList android_version = new ArrayList<>();
for(int i=0;i<android_version_names.length;i++){
AndroidVersion androidVersion = new AndroidVersion();
androidVersion.setAndroid_version_name(android_version_names[i]);
androidVersion.setAndroid_image_url(android_image_urls[i]);
android_version.add(androidVersion);
}
return android_version;
}
}
There are some other libraries such as Fresco, Glide for Image loading. You can try that too.
Image Credits : Google
Enjoy Coding 🙂