Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
GridView Using Custom ArrayAdapter in Android with Example
Next article icon

GridView Using Custom ArrayAdapter in Android with Example

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

This article will be building an application to demonstrate the use of CustomArrayAdapters in GridView. GridViews are view containers that display views in two-dimensional (rows and columns) they are used in my android applications one simple example would be the gallery app. The adapter connects the UI elements with the data source, It helps populate data with us taking care of it. e.g when the user is scrolling through a gallery app the GridView is automatically populated without specifying anything.

There are different Adapters to name a few:

  • ArrayAdapter 
  • BaseAdapter 
  • Custom ArrayAdapter 

This article will be covering the Custom ArrayAdapter.

Why use a Custom ArrayAdapter? 

Android already provides the implementation for an ArrayAdapter, which can be used with just a single line demonstrated below. They are used when Whenever we have a list of single items we can use ArrayAdapter. For instance, a list of phone contacts, countries, or names. Below is the syntax of the ArrayAdapter used for displaying text data.

Syntax:

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

Parameters:

  • Context: is the application context being used 
  • resource: The second parameter is the resource id used to set the layout(xml file). 
  • textViewResourceId: Id of the TextView element where the data will be displayed 
  • objects: are the data elements stored in an array.

The problem with or limitation of this method is we cannot use complex layouts eg. Imagine we were building an app like Netflix or prime where each element is made up of many elements eg an ImageView, TextView, etc. Such complex views are not possible with the simple implementation of the ArrayAdapter for this we need to create our custom Adapter by extending the ArrayAdapter class. The following code shows the structure of the Custom ArrayAdapter.

Java
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import java.util.List;  public class CustomAdapter extends ArrayAdapter {     public CustomAdapter(Context context, int resource,                          List objects)     {         super(context, resource, objects);     }      @Override public int getCount()     {         return super.getCount();     }      @Override     public View getView(int position, View convertView,                         ViewGroup parent)     {         return super.getView(position, convertView, parent);     } } 
Kotlin
import android.content.Context import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter  class CustomAdapter(context: Context?, resource: Int, objects: List<*>?) : ArrayAdapter<Any?>(context!!, resource, objects!!) {          override fun getCount(): Int {         return super.getCount()     }      override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {         return super.getView(position, convertView, parent)     } } 

The two important methods used are:

  1. getCount(): returns the number of data elements present in the List.
  2. getView(): this is the important method where will be initializing and returning the custom view.

Example

A sample GIF is given below to get an idea about what we are going to do in this article. We are going to implement this project using both Java and Kotlin Programming Language for Android. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout      xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity">      <GridView         android:id="@+id/grid_view"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:horizontalSpacing="10dp"         android:numColumns="auto_fit"         android:verticalSpacing="10dp"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout> 

Creating a new layout XML file 

Creating a new file named custom_view.xml in the same folder as the activity_main.xml file. This Custom View will host an ImageView and a TextView.

XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <ImageView         android:id="@+id/imageView"         android:layout_width="match_parent"         android:layout_height="wrap_content"         tools:srcCompat="@tools:sample/avatars" />      <TextView         android:id="@+id/textView"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="TextView"         android:textAlignment="center" /> </LinearLayout> 

Step 3: Working with the Java/Kotlin Files

Now to hold the info of our custom view we need to create a getter setter class. We can do things without this just using two arrays one for images and the other for text but what if we want to modify your view to add a Button? Then will again need a third array. Hence a class that holds the custom view is a more flexible option. Create the class In the same folder as the MainActivity create a new class called items. Below is the code for the items file.

Java
public class items {      private int image_id;     private String text;      public int getImage_id() {         return image_id;     }      public void setImage_id(int image_id) {         this.image_id = image_id;     }      public String getText() {         return text;     }      public void setText(String text) {         this.text = text;     }      items(int img, String text) {         image_id = img;         this.text = text;     } } 
Kotlin
class items internal constructor(private var image_id: Int, private var text: String) {     fun getImage_id(): Int {         return image_id     }      fun setImage_id(image_id: Int) {         this.image_id = image_id     }      fun getText(): String {         return text     }      fun setText(text: String) {         this.text = text     }      fun items(img: Int, text: String) {         image_id = img         this.text = text     } } 

Note: For an image, we store the id of the image and not the image.

Step 4: Implementing the CustomAdapter class

Create a new class classed as CustomAdapter in the same folder as items. The main method of the Adapter is the getView(). 

Java
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable;  @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {      View v = convertView;      if (v == null) {         // getting reference to the main layout and initializing         LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);         v = inflater.inflate(custom_layout_id, null);     }      // initializing the imageview and textview and setting data     ImageView imageView = v.findViewById(R.id.imageView);     TextView textView = v.findViewById(R.id.textView);      // get the item using the  position param     items item = items_list.get(position);      imageView.setImageResource(item.getImage_id());     textView.setText(item.getText());     return v; } 
Kotlin
import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView  fun getView(position: Int, convertView: View?, parent: ViewGroup): View {              var v = convertView              if (v == null) {         // getting reference to the main layout and initializing         val inflater = getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater         v = inflater.inflate(custom_layout_id, null)     }      // initializing the imageview and textview and setting data     val imageView = v!!.findViewById<ImageView>(R.id.imageView)     val textView = v.findViewById<TextView>(R.id.textView)      // get the item using the  position param     val item: items = items_list.get(position)     imageView.setImageResource(item.getImage_id())     textView.setText(item.getText())     return v } 
public View inflate (int resource,ViewGroup root)

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

resource  int: ID for an XML layout resource to load (e.g., R.layout.main_page)

root  ViewGroup: Optional view to be the parent of the generated hierarchy. This value may be null.

This method first parses out the custom_view.xml file using the inflate() method. This method is quite computationally expensive and should be used only when needed eg. when the adapter has to populate new items when the user reaches the end of the screen. After getting the reference of the view we then initialize the image view and text view from the custom_view.xml file set the data and return the view. Below is the complete code for the CustomAdapter file. Comments are added inside the code to understand the code in more detail.

Java
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable;  public class CustomAdapter extends ArrayAdapter<items> {      List<items> items_list;     int custom_layout_id;      public CustomAdapter(@NonNull Context context, int resource, @NonNull List<items> objects) {         super(context, resource, objects);         items_list = objects;         custom_layout_id = resource;     }      @Override public int getCount() {         return items_list.size();     }      @NonNull     @Override     public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {         View v = convertView;         if (v == null) {             // getting reference to the main layout and initializing             LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);             v = inflater.inflate(custom_layout_id, null);         }          // initializing the imageview and textview and setting data         ImageView imageView = v.findViewById(R.id.imageView);         TextView textView = v.findViewById(R.id.textView);          // get the item using the  position param         items item = items_list.get(position);          imageView.setImageResource(item.getImage_id());         textView.setText(item.getText());         return v;     } } 
Kotlin
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  class CustomAdapter(context: Context, resource: Int, objects: List<items>) : ArrayAdapter<items?>(context, resource, objects) {      init {         items_list = objects         custom_layout_id = resource     }          private var items_list: List<items>     private var custom_layout_id: Int          override fun getCount(): Int {         return items_list.size     }      fun getView(position: Int, convertView: View?, parent: ViewGroup): View {         var v = convertView         if (v == null) {             // getting reference to the main layout and initializing             val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater             v = inflater.inflate(custom_layout_id, null)         }          // initializing the imageview and textview and setting data         val imageView = v!!.findViewById<ImageView>(R.id.imageView)         val textView = v.findViewById<TextView>(R.id.textView)          // get the item using the  position param         val item: items = items_list[position]         imageView.setImageResource(item.getImage_id())         textView.setText(item.getText())         return v     } } 

Step 5: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. For the images, here we have used android studio icons. The images are present in the res/drawable folder. Should look like this 

Java
import android.os.Bundle; import android.widget.GridView; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList;  public class MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          List<items> itemsList = new ArrayList<>();         itemsList.add(new items(R.drawable.android_1, "image_1"));         itemsList.add(new items(R.drawable.android_2, "image_2"));         itemsList.add(new items(R.drawable.android_3, "image_3"));         itemsList.add(new items(R.drawable.android_4, "image_4"));         itemsList.add(new items(R.drawable.android_5, "image_5"));         itemsList.add(new items(R.drawable.android_1, "image_6"));         itemsList.add(new items(R.drawable.android_2, "image_7"));         itemsList.add(new items(R.drawable.android_3, "image_8"));         itemsList.add(new items(R.drawable.android_4, "image_9"));         itemsList.add(new items(R.drawable.android_5, "image_10"));          GridView gridView = findViewById(R.id.grid_view);         CustomAdapter customAdapter = new CustomAdapter(this, R.layout.custom_view, itemsList);         gridView.setAdapter(customAdapter);     } } 
Kotlin
import android.os.Bundle import android.widget.GridView import androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)                  val itemsList: List<items> = ArrayList<Any>()         itemsList.add(items(R.drawable.android_1, "image_1"))         itemsList.add(items(R.drawable.android_2, "image_2"))         itemsList.add(items(R.drawable.android_3, "image_3"))         itemsList.add(items(R.drawable.android_4, "image_4"))         itemsList.add(items(R.drawable.android_5, "image_5"))         itemsList.add(items(R.drawable.android_1, "image_6"))         itemsList.add(items(R.drawable.android_2, "image_7"))         itemsList.add(items(R.drawable.android_3, "image_8"))         itemsList.add(items(R.drawable.android_4, "image_9"))         itemsList.add(items(R.drawable.android_5, "image_10"))                  val gridView = findViewById<GridView>(R.id.grid_view)         val customAdapter = CustomAdapter(this, R.layout.custom_view, itemsList)         gridView.adapter = customAdapter     } } 

Final Project Structure

Output:


Next Article
GridView Using Custom ArrayAdapter in Android with Example

C

clivefernandes777
Improve
Article Tags :
  • Java
  • Kotlin
  • Android
  • Android-View
Practice Tags :
  • Java

Similar Reads

    GridView Using BaseAdapter in Android with Example
    Here, we are designing an Android app to demonstrate the use of GridView layout. The GridView layout is a ViewGroup that groups view in a two-dimensional scrolling grid of rows and columns. Items in the grid view are inserted using a ListAdapter. The layout by default is scrollable and we don't need
    6 min read
    ArrayAdapter in Android with Example
    The Adapter acts as a bridge between the UI Component and the Data Source. It converts data from the data sources into view items that can be displayed into the UI Component. Data Source can be Arrays, HashMap, Database, etc. and UI Components can be ListView, GridView, Spinner, etc. ArrayAdapter is
    3 min read
    Custom ArrayAdapter with ListView in Android
    In the previous article ArrayAdapter in Android with Example, it's been discussed how the ArrayAdapter works and what are the data sources which can be attached to the ArrayAdapter with ListView. In this article, it's been discussed how to implement custom ArrayAdapter with the ListView. Have a look
    7 min read
    CustomArrayAdapter in Android with Example
    In Android, ArrayAdapters are used for populating and controlling the ListView and Spinners. ArrayAdapter by default provides List-Items that include only single information or single TextView. In order to have a more complex layout that includes multiple information in a single List-Item such as im
    6 min read
    GridView in Android with Example
    A GridView is a type of AdapterView that displays items in a two-dimensional scrolling grid. Items are inserted into this grid layout from a database or from an array. The adapter is used for displaying this data, setAdapter() method is used to join the adapter with GridView. The main function of th
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences