Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

CustomArrayAdapter in Android with Example

An ArrayAdapter is a type of Adapter in Android used for populating UI components like ListView or GridView from an array of data. A CustomArrayAdapter allows us to inflate custom views for each item instead of using a generic view.

Here's how you can create a CustomArrayAdapter:

1. Create a data model:

First, let's assume we have a simple data model named Person:

data class Person(val name: String, val age: Int)

2. Design a custom layout:

Create a new XML layout file, person_item.xml, which will define the layout for each Person in the list:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tvAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

</LinearLayout>

3. Create the Custom ArrayAdapter:

class CustomArrayAdapter(context: Context, private val resource: Int, private val items: List<Person>) : ArrayAdapter<Person>(context, resource, items) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view: View = convertView ?: LayoutInflater.from(context).inflate(resource, parent, false)

        val tvName: TextView = view.findViewById(R.id.tvName)
        val tvAge: TextView = view.findViewById(R.id.tvAge)

        val person = getItem(position)
        tvName.text = person?.name
        tvAge.text = person?.age.toString()

        return view
    }
}

4. Use the CustomArrayAdapter:

In your activity or fragment:

val persons = listOf(Person("John", 28), Person("Doe", 25))
val adapter = CustomArrayAdapter(this, R.layout.person_item, persons)
val listView: ListView = findViewById(R.id.listView)
listView.adapter = adapter

This way, you can customize each item of the ListView with your custom view and data model.

  1. Creating a CustomArrayAdapter in Android Kotlin:

    • Description: Creating a CustomArrayAdapter involves extending the ArrayAdapter class and customizing its behavior to handle specific data and layout requirements. This allows you to have more control over the appearance and behavior of items in a ListView.
    • Code (Kotlin):
      // Example of creating a CustomArrayAdapter
      class CustomArrayAdapter(context: Context, resource: Int, objects: List<String>) :
          ArrayAdapter<String>(context, resource, objects) {
      
          override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
              // Implement custom view creation logic here
              // ...
              return customView
          }
      }
      
  2. Customizing ArrayAdapter in Android for custom layouts:

    • Description: Customizing ArrayAdapter for custom layouts involves inflating a custom layout for each item in the ArrayAdapter. This allows you to design and control the appearance of each item in the ListView.
    • Code (Kotlin):
      // Example of customizing ArrayAdapter for custom layouts
      class CustomArrayAdapter(context: Context, resource: Int, objects: List<CustomItem>) :
          ArrayAdapter<CustomItem>(context, resource, objects) {
      
          override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
              val inflater = LayoutInflater.from(context)
              val customView = inflater.inflate(R.layout.custom_item_layout, parent, false)
              // Populate customView with data from CustomItem
              // ...
              return customView
          }
      }
      
  3. Populating ListView with CustomArrayAdapter in Android:

    • Description: Populating a ListView with a CustomArrayAdapter involves creating an instance of the adapter and setting it to the ListView. The adapter is responsible for providing the views for each item in the list.
    • Code (Kotlin):
      // Example of populating ListView with CustomArrayAdapter
      val customItems = listOf("Item 1", "Item 2", "Item 3")
      val customArrayAdapter = CustomArrayAdapter(this, R.layout.custom_item_layout, customItems)
      val listView: ListView = findViewById(R.id.listView)
      listView.adapter = customArrayAdapter
      
  4. Handling click events with Custom ArrayAdapter:

    • Description: Handling click events with a Custom ArrayAdapter involves setting an item click listener on the ListView. This allows you to respond to user clicks on individual items.
    • Code (Kotlin):
      // Example of handling click events with Custom ArrayAdapter
      listView.setOnItemClickListener { parent, view, position, id ->
          // Handle click on item at position
          val selectedItem = customItems[position]
          // Perform actions based on the clicked item
      }
      
  5. Customizing data display in Android ArrayAdapter:

    • Description: Customizing data display in ArrayAdapter involves implementing the getView method to control how data is presented in each item view. This can include formatting text, displaying images, or using custom layouts.
    • Code (Kotlin):
      // Example of customizing data display in ArrayAdapter
      class CustomArrayAdapter(context: Context, resource: Int, objects: List<DataItem>) :
          ArrayAdapter<DataItem>(context, resource, objects) {
      
          override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
              val inflater = LayoutInflater.from(context)
              val customView = inflater.inflate(R.layout.custom_item_layout, parent, false)
              val dataItem = getItem(position)
              // Populate customView with data from DataItem
              // ...
              return customView
          }
      }
      
  6. Using ArrayAdapter with custom objects in Android:

    • Description: Using ArrayAdapter with custom objects involves creating an ArrayAdapter that works with a specific data type (e.g., custom objects). The getView method is then customized to handle the display of these custom objects.
    • Code (Kotlin):
      // Example of using ArrayAdapter with custom objects
      class CustomArrayAdapter(context: Context, resource: Int, objects: List<CustomObject>) :
          ArrayAdapter<CustomObject>(context, resource, objects) {
      
          override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
              val inflater = LayoutInflater.from(context)
              val customView = inflater.inflate(R.layout.custom_item_layout, parent, false)
              val customObject = getItem(position)
              // Populate customView with data from CustomObject
              // ...
              return customView
          }
      }
      
  7. Customizing text and images in CustomArrayAdapter:

    • Description: Customizing text and images in CustomArrayAdapter involves updating the layout and populating views with text and image data from the underlying data source (e.g., custom objects).
    • Code (Kotlin):
      // Example of customizing text and images in CustomArrayAdapter
      class CustomArrayAdapter(context: Context, resource: Int, objects: List<CustomItem>) :
          ArrayAdapter<CustomItem>(context, resource, objects) {
      
          override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
              val inflater = LayoutInflater.from(context)
              val customView = inflater.inflate(R.layout.custom_item_layout, parent, false)
              val customItem = getItem(position)
              
              val textView: TextView = customView.findViewById(R.id.textView)
              val imageView: ImageView = customView.findViewById(R.id.imageView)
              
              textView.text = customItem?.text
              imageView.setImageResource(customItem?.imageRes ?: R.drawable.default_image)
              
              return customView
          }
      }
      
  8. Sorting data with Custom ArrayAdapter in Android:

    • Description: Sorting data with a Custom ArrayAdapter involves implementing sorting logic within the adapter. This can be achieved by modifying the data source and notifying the adapter of the changes.
    • Code (Kotlin):
      // Example of sorting data with Custom ArrayAdapter
      fun sortData() {
          customItems.sortBy { it.text }
          customArrayAdapter.notifyDataSetChanged()
      }