Android Snippet – Custom Adapter with ViewHolder Pattern in Android

Android Snippet – Custom Adapter with ViewHolder Pattern in Android

In this android code snippet, we are going to learn how to create a custom adapter with ViewHolder Pattern in android application development.

The ViewHolder Pattern helps to increase the loading speed of ListView items because it does not call the findViewById method repeatedly.

Once the View is set, the ViewHolder object is set with View’s setTag method and in this case either in the layout will inflated or the findViewById method called.

If you check our other ListView android snippet example that does make use of the a custom adapter, you will see that both the layout inflator and findViewById method are repeatedly called. This can cause a performance issue when you have thousands of data to load in your ListView

Before we start, the first thing I will do is to list the environment and tools I used in this android code snippet but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: CustomAdapterWithViewHolder

Package: com.inducesmile.customadapterwithviewholder

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, copy and paste the following code snippet to your project. Replace the code in your file with the following code.

Make sure you change the package name if you did not use the same package.


import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {

private ListView customListView;


protected void onCreate(Bundle savedInstanceState) {



customListView = (ListView)findViewById(;

List<ItemObject> listViewItems = new ArrayList<ItemObject>();

listViewItems.add(new ItemObject("Nigeria"));

listViewItems.add(new ItemObject("Ghana"));

listViewItems.add(new ItemObject("Senegal"));

listViewItems.add(new ItemObject("Togo"));

listViewItems.add(new ItemObject("Kenya"));

customListView.setAdapter(new CustomAdapter(this, listViewItems));

customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

// make Toast when click

Toast.makeText(getApplicationContext(),"Position " + position, Toast.LENGTH_LONG).show();



public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(, menu);

return true;



public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == {

return true;


return super.onOptionsItemSelected(item);



In the above code, we have created a custom adapter that uses ViewHolder pattern. The custom adapter is passed to the ListView setAdapter method with a List object that contains all the data the ListView will display.

Below is the code snippet that detailed how how the ViewHolder pattern was used.


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;

public class CustomAdapter extends BaseAdapter {

private LayoutInflater lInflater;

private List<ItemObject> listStorage;

public CustomAdapter(Context context, List<ItemObject> customizedListView) {

lInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

listStorage = customizedListView;



public int getCount() {

return listStorage.size();



public Object getItem(int position) {

return position;



public long getItemId(int position) {

return position;



public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder listViewHolder;

if(convertView == null){

listViewHolder = new ViewHolder();

convertView = lInflater.inflate(R.layout.list, parent, false);

listViewHolder.textInListView = (TextView)convertView.findViewById(;



listViewHolder = (ViewHolder)convertView.getTag();



return convertView;


static class ViewHolder{

TextView textInListView;



The main XMl file for the UI interface and the inflated layout file is shown below.

<RelativeLayout xmlns:android=""

xmlns:tools="" android:layout_width="match_parent"

android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"



android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">






android:layout_centerHorizontal="true" />



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""

android:layout_width="match_parent" android:layout_height="match_parent">









android:id="@+id/textView" />



Save the file and run your project. If everything works for you, an image like this will appear on your test device.


You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

No Responses

Add a Comment