How to Use Picasso Android Library?

Step 1: Create an empty activity project

Create an empty activity Android Studio project. Refer to Android | How to Create/Start a New Project in Android Studio? to know how to create an empty activity Android Studio project. Note that select Java as the programming language.

Step 2: Adding the required dependency to app-level gradle file

For using Picasso in the android project, we have to add a dependency in the app-level gradle file. So, For adding dependency open app/build.gradle file in the app folder in your Android project and add the following lines inside it. Add these lines inside dependencies{}.

implementation ‘com.squareup.picasso:picasso:2.8’ 

Refer to the following image, if unable to locate the app-level gradle file and invoke the dependency.

Now click on the “Sync Now” button. So that the Android Studio downloads the required dependency files. If you get any type of error then you may check the error on stackoverflow.

Step 3: Working with the Manifest File

Now add InternetPermission inside the AndroidManifest.xml file. Open the manifest.xml file and add the following line. 

<users-permission android:name=”android.permission.INTERNET”/>

Step 4: Working with the activity_main.xml file

Open the layout file for the activity_main.xml file. We need to add an ImageView to the application’s main layout.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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:background="@color/colorAccent"
    tools:context=".MainActivity">
  
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:padding="16dp" />
  
</RelativeLayout>


Step 5: Working with the MainActivity.java file

Now Navigate to the MainActivity.java file and add the following block of code. In the first line, we are getting the ImageView instance from the layout. And then load the image from the remote URL mentioned in the JAVA code using the Picasso library.

Java




import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.widget.ImageView;
  
import com.squareup.picasso.Picasso;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        ImageView imageView = findViewById(R.id.imageView);
        Picasso.with(this)
                .load("https://media.w3wiki.org/wp-content/cdn-uploads/logo-new-2.svg")
                .into(imageView);
    }
}


Output:

Note: Make sure the mobile device or Android Emulator has the network connectivity to load the image.

How to Use Picasso Image Loader Library in Android?

Picasso is open source and one of the widely used image download libraries in Android. It is created and maintained by Square. It is among the powerful image download and caching library for Android. Picasso simplifies the process of loading images from external URLs and displays them on your application. For example, downloading an image from the server is one of the most common tasks in any application. And it needs quite a larger amount of code to achieve this via android networking API. By using Picasso, you can achieve this with a few lines of code. 

Similar Reads

How to Use Picasso Android Library?

Step 1: Create an empty activity project...

More Functionalities of the Picasso Library

...

Troubleshooting while loading an image using Picasso Library

...

Contact Us