How to Display Image on ImageView with Image Url in Android

In this android programming code example, we will illustrate how easy it is to display image on ImageView with image url in android.

There are different ways you can use to show remote image on ImageView but here we will make use of Picasso which is an android network library.

Steps

  1. Add an empty ImageView component in XML layout file
  2. Get the instance of the ImageView in the Activity class.
  3. Add Internet Uses-Permission since we will make a call over the internet
  4. Add Picasso library in your project gradle module
  5. Download the remote image using Picasso and image url
  6. Display or show image on ImageView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="400dp"
        android:layout_height="300dp"
        android:scaleType="centerCrop"
        android:contentDescription="@string/app_name"/>

</LinearLayout>

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

build.gradle

//Picasso
implementation 'com.squareup.picasso:picasso:2.71828'

MainActivity.java

import android.os.Bundle;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = findViewById(R.id.imageView2);
        String imageUrl = "https://via.placeholder.com/500";

        //Loading image using Picasso
        Picasso.get().load(imageUrl).into(imageView);
    }
}

android imageview picaso

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.