Volley- singleton pattern in android with easy explanation

Volley is an HTTP library that provides the very fast and easy networking connection for the android application without writing tons of code.

Using volley,we can easily hit the API and get the data from the server.

Features of volley:

1.Send request synchronous and asynchronous

2.Prioriization  of request

3.Queuing of request

4.Cancelling the request

5.Effective request cache  and memory management

6. Extensibility and customization of the library according to our needs 


1.Automatic image loading 

2.Caching mechanism

3.Multiple concurrent network connection

4.Retry policy

Volley-Singleton Class Pattern

1.Adding permission inside the AndroidManifest.xml

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

2.Adding dependency in build.gradle(Module:app)

Implementation ‘com.android.volley:volley:1.0.0’

Now,Let’s start about Volley-singleton pattern

Volley singleton-pattern is class in which we contains the code of RequestQueue,Request and ImageLoader  at one place & This class can be used anywhere in the entire project.

Advantage of singleton-pattern class

1. Resuability of code

2.Saving the time

3.Keeping safe the code


There most three classes of volley-singleton pattern


It is used to dispatching the request to the network.It is created because we can use as singleton


 It is base class which is used to create network request such as (POST,GET).


It is a helper class which is used to loading and caching the image from the URLs.

Volley-singleton Class is given below:


import android.context,Context;
import android.graphics.Bitmap;
import android.support.v4.util.Lrucache:
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.volley;

public class VolleySingleton
Private static VolleySingleton singletonInsatnce;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private static Context context;

Private VolleySingleton(Context context)
new ImageLoader(this.requestQueue,new ImageLoader.ImageCache( )
private final LruCache<String,Bitmap> lrucache=new LruCache<String,Bitmap>(20);

//Here we can 20  number of entries in the cache maximum
Public void PutBitmap(String url,Bitmap bitmap)
public Bitmap  getBitmap(string url)
Return lrucache.get(url); 

public static VolleySingletion getInstance(Context context)
singletonInsatnce=new VolleySingletion(context);
return singletonInsatnce;

public RequestQueue getRequestQueue( )
return this.requestQueue;

public void addToRequestQueue(Request req)
getRequestQueue( ).add(req);

public ImageLoader getImageLoader( )
return this.imageLoader;

Now we will learn how VolleySinleton.java class can be used in activity for the RequestQueue() and ImageLoader( ).  

1.Setting the  RequestQueue  in the activity or where you want to use

//Getting the RequestQueue

RequestQueue requestQueue=VolleySingleton.getInstance(this.getApplicationContext( )).getRequestQueue( );

//Adding request in a queue(Example calling the “stringRequest”) to the RequestQueue.


2.Setting the code for ImageLoading in the activity or where you want to use

Private Imageloader imageLoader;
private NetworkImageView  imageview;

protected void onCreate(Bundle SavedInstanceState)
String url=”https://your_image_url;
imageLoader=VolleySingleton.getinstance(getApplicationContext( )).getImageLoader( );

//imageview means where you will display the image
NetworkImageView imageview=(NetworkImageView)findViewById(R.id.imageView);



Hope,I explained all the topics  step by step about the volley singleton pattern which will help you fetch the data from the server very easily and quickly.If you have any query then you can comment me.


Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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