Android Volley ImageLoader and NetworkImageView Example

Photo Credit: Giandomenico Ricci - Android Volley ImageLoader

One of the new concepts introduced in Android is Volley. The reason, volley library is preferred over other similar libraries is because it is authored by Google engineers. But one of the major problems with volley library is that it lacks proper documentation. As far as I know the only official documentation available is at this Android Developers page. Coming to the point, here I will discuss how to make an Android Volley ImageLoader Example using the NetworkImageView. As in one of my earlier tutorials, we discussed how to make asynchronous HTTP calls through Volley, I will not go in details of Volley. Volley is a highly customizable library and can be used in many situations like, displaying images in a ListView, RecyclerView, or just getting plain data from REST web APIs. Here we will discuss the basic building blocks of volley which may help you to retrieve images from the network.

To start off with volley first you may need to download and include Android volley library in your project. For steps please refer to this volley tutorial. Next you may need to add the internet permission in your manifest:

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

How to load images through Android Volley ImageLoader?

Earlier to load images in an Android ImageView, the standard approach taken was, the usage of HttpURLConnection class. But now with volley this is simplified as we may not need to get in to such complex code. Volley gives us two new classes which make life easier.

Android Volley ImageLoader

Volley uses ImageLoader to load images from network, and also to cache them into your Android phone’s in-memory cache by using the LruCache class. A good approach would be to use Android ImageLoader in a singleton pattern, as same cache should be used throughout the application. Also if used in a singleton pattern your cache would not be activity dependent. Hence whenever you close and open the activity, your images could be picked up from the cache, saving network requests. Now this brings up a question, how to make this volley singleton class? please have a look at the code below:

package com.truiton.volleyimageloader;

import android.content.Context;
import android.util.LruCache;


 * Custom implementation of Volley Request Queue
public class CustomVolleyRequestQueue {

    private static CustomVolleyRequestQueue mInstance;
    private static Context mCtx;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private CustomVolleyRequestQueue(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<String, Bitmap>(20);

                    public Bitmap getBitmap(String url) {
                        return cache.get(url);

                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);

    public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new CustomVolleyRequestQueue(context);
        return mInstance;

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
            Network network = new BasicNetwork(new HurlStack());
            mRequestQueue = new RequestQueue(cache, network);
            // Don't forget to start the volley request queue
        return mRequestQueue;

    public ImageLoader getImageLoader() {
        return mImageLoader;


In the above class you may see that a CustomVolleyRequestQueue is set up which can return an object of ImageLoader. The class above also creates an ImageCache which works with normal volley cache, but only caching images for requests. Please note here: I just modified the singleton class from my previous Volley Example. Therefore as a whole, the above class can be used to make any type of Android volley requests, be it a JsonRequest, ImageRequest, or just a simple StringRequest.

Android Volley NetworkImageView

A new view introduced with Android volley is NetworkImageView. As a matter of fact this view replaces the standard ImageView, when comes to the usage of volley. Android Volley NetworkImageView is designed in such a way that it creates an image request, sends it and even displays the image, after it is downloaded from the URL. Also it cancels the request on its own if it is detached from the view hierarchy. Hence no need to handle the request cancellations with Volley NetworkImageView. Have a look at the layout for this Android Volley ImageLoader and NetworkImageView Example, it just includes a NetworkImageView instead of ImageView:

<RelativeLayout xmlns:android=""



Next lets have a look at the code for the main activity class:

package com.truiton.volleyimageloader;

import android.os.Bundle;


public class MainActivity extends AppCompatActivity {
    private NetworkImageView mNetworkImageView;
    private ImageLoader mImageLoader;

    protected void onCreate(Bundle savedInstanceState) {
        mNetworkImageView = (NetworkImageView) findViewById(

    protected void onStart() {
        // Instantiate the RequestQueue.
        mImageLoader = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
        //Image URL - This can point to any image file supported by Android
        final String url = "";
        mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView,
                R.mipmap.truiton_short, android.R.drawable
        mNetworkImageView.setImageUrl(url, mImageLoader);

As you may see in the above class I used an instance of CustomVolleyRequestQueue to get the volley ImageLoader object. Further I just specified the URL through setImageUrl method of NetworkImageView. This handles all the functionality by itself.

Want to display images in a ListView using volley?

If you need to display images in a ListView just use the setImageUrl method of NetworkImageView in the getView method of your adapter. This will handle all your image requests automatically. Just a reminder, don’t forget to use the  NetworkImageView instead of normal ImageView.

Android Volley NetworkImageView

Screenshot of Android Volley ImageLoader Example:

For better understanding, please refer to full source code below:

Full Source Code

Default images in Android Volley NetworkImageView

There are situations when you may just want to simply display a placeholder for an image which is about to be fetched from the the network. With Android Volley ImageLoader this can be done very easily. It gives a get method, through which you can display a default image as well as an error image in-case the request fails. In this ImageLoader tutorial I have used R.mipmap.truiton_short as a default image and android.R.drawable.ic_dialog_alert as an error image in the main activity class above. Hope this helped you. Please connect with us on Facebook, Google+, and Twitter for more updates.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

Your email address will not be published. Required fields are marked *

11 thoughts on “Android Volley ImageLoader and NetworkImageView Example

  • Mahdi

    Thanks to document. by Picasso we have some ability to display image such as : resize, centerCrop and create animation on onSuccess method. can we merge Volly and Picasso to have or create this ability?

    please Notify me of new posts by email.

  • Bharat


    How to requestimage using Volley. I want to use volley When I capture image from camera and store it in server using PHP Code.

    Can you help me on this?

  • Sharib Tanweer

    This Volley Not Working In Eclipse Can You Explain More?

    I Have Ubantu Linux O/S In Which I Have Eclipse Is = Android Developer Tools And Build: v22.6.2-1085508.

    So How Can I Use Volley In My Project?

  • Spiros

    Nice tutorial. This works fine in my app. I have one question, in your code on CustomVolleyRequesQueue you use getBitmap and putBitmap . When we use Networked image we use internet and the url in order to cache the image. How you display the images without internet connection. If you downloaded once you have all images in cache.right? So, how you display these images from cache with just using the url. there is any way for this?

  • Anubhav

    Superb article. It worked like charm.
    I am using volley to load JSON from server but everytime it fetches data from server. I want the data to be cached. Just like the ImageLoader.
    U said we can use the same class to make a JSON request. Can u please help me a little on how to make a JSON request??