Skip to content
Related Articles

Related Articles

Improve Article

CustomArrayAdapter in Android with Example

  • Difficulty Level : Basic
  • Last Updated : 24 Nov, 2020

In Android, ArrayAdapters are used for populating and controlling the ListView and Spinners. ArrayAdapter by default provides ListItems that include only single information or single TextView. In order to have a more complex layout that includes multiple information in a single ListItem such as images, text, etc. we use CustomArrayAdapter. Apps like Instagram, WhatsApp, and many more are using such a complex layout.


In this example, we are going to make an app for different versions of Android OS which is having an ImageView and a TextView which shows the version name. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

CustomArrayAdapter in Android

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. . Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

In this step, we will add a ListView to our activity_main.xml file which is used to show the data of listItems. Go to the app > res > layout > activity_main.xml and the following code snippet.


<?xml version="1.0" encoding="utf-8"?>
        android:dividerHeight="2dp" />

Before moving further let’s add some color attributes in order to enhance the app bar. Go to app > res > values > colors.xml and add the following color attributes. 


    <color name="colorPrimary">#0F9D58</color
    <color name="colorPrimaryDark">#16E37F</color
    <color name="colorAccent">#03DAC5</color

Step 3: Create a new layout file list_item.xml

In this step, we will create a layout file for the single list item view. Go to app > res > layout > right-click > New > Layout Resource File and name it as list_item. list_item.xml contains an ImageView and a TextView which is used for populating the ListView.


<?xml version="1.0" encoding="utf-8"?>
    <!--For image src we have used ic_launcher
        and for text "GeeksForGeeks" and "gfg" 
        they are used only for reference how it will looks"-->
        android:src="@mipmap/ic_launcher_round" />
            android:textStyle="bold" />
            android:text="gfg" />

Step 4: Create a new Java class

We will create a new java class and name it as Item. contains three private member variables androidVersionImage, androidVersionName, androidVersionNumber. Later on, we will create an ArrayList of this Item type. Go to app > java > package > right-click > create new java class.


public class Item {
    private int androidVersionImage;
    private String androidVersionName;
    private String androidVersionNumber;
    // Constructor
    public Item(int androidVersionImage, String androidVersionName, String androidVersionNumber) {
        this.androidVersionImage = androidVersionImage;
        this.androidVersionName = androidVersionName;
        this.androidVersionNumber = androidVersionNumber;
    // Getters and Setters method
    public int getAndroidVersionImage() {
        return androidVersionImage;
    public void setAndroidVersionImage(int androidVersionImage) {
        this.androidVersionImage = androidVersionImage;
    public String getAndroidVersionName() {
        return androidVersionName;
    public void setAndroidVersionName(String androidVersionName) {
        this.androidVersionName = androidVersionName;
    public String getAndroidVersionNumber() {
        return androidVersionNumber;
    public void setAndroidVersionNumber(String androidVersionNumber) {
        this.androidVersionNumber = androidVersionNumber;

Step 5: Creating Adapter class

Now, we will create an Adapter class that acts as a bridge between the UI Component and the Data Source .i.e., androidVersionImage, androidVersionName, androidVersionNumber, and ListView. Go to the app > java > package > right-click and create a new java class and name it as Adapter. Below is the code snippet is given for it.


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class Adapter extends ArrayAdapter <Item> {
    ImageView imageView;
    TextView textView1, textView2;
    ArrayList <Item> androidVersionList = new ArrayList <>();
    public Adapter(Context context, int textViewResourceId, ArrayList <Item> objects) {
        super(context, textViewResourceId, objects);
        androidVersionList = objects;
    // Returns total number of items to be displayed in the list.
    // It counts the value from the arraylist size
    public int getCount() {
        return super.getCount();
    // This function implicitly gets called when the listItem view is ready
    // to be displayed. Here we set the layout and add data to the views
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        View view = convertView;
          // Setting layout
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_item, null);
        imageView = (ImageView) view.findViewById(;
        textView1 = (TextView) view.findViewById(;
        textView2 = (TextView) view.findViewById(;
           // Adding data to the Views
        return view;

Step 6: Working with the file

In class we create an ArrayList for storing images and texts. These images are placed in the drawable folder(app > res > drawable). You can use any images in place of this. We get the reference of listView and set the adapter on the listView.


import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    ListView listView;
    ArrayList <Item> androidVersionList = new ArrayList <>();
    protected void onCreate(Bundle savedInstanceState) {
          // Getting the reference of listView
        listView = (ListView) findViewById(;
          // Adding image and texts to list
        androidVersionList.add(new Item(R.drawable.donut, "Donut", "1.6"));
        androidVersionList.add(new Item(R.drawable.eclair, "Eclair", "2.0 - 2.1"));
        androidVersionList.add(new Item(R.drawable.froyo, "Froyo", "2.2 - 2.2.3"));
        androidVersionList.add(new Item(R.drawable.gingerbread, "GingerBreak", "2.3 - 2.3.7"));
        androidVersionList.add(new Item(R.drawable.honeycomb, "HoneyComb", "3.0 - 3.2.6"));
        androidVersionList.add(new Item(R.drawable.icecream, "IceCream", "4.0 - 4.0.4"));
        androidVersionList.add(new Item(R.drawable.jellybean, "JellyBean", "4.1 - 4.3.1"));
        androidVersionList.add(new Item(R.drawable.kitkat, "KitKat", "4.4 - 4.4.4"));
        androidVersionList.add(new Item(R.drawable.lollipop, "Lollipop", "5.0 - 5.1.1"));
        androidVersionList.add(new Item(R.drawable.marshmallow, "Marshmallow", "6.0 - 6.0.1"));
        Adapter adapter = new Adapter(this, R.layout.list_item, androidVersionList);
          // Setting the adapter to list

Output: Run On Emulator 

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

My Personal Notes arrow_drop_up
Recommended Articles
Page :