Skip to main content

How to implement an expandable ListView in Kotlin Android

How to implement an expandable ListView in Kotlin Android.

Here is a step-by-step tutorial on how to implement an expandable ListView in Kotlin for Android.

Step 1: Set up the project

  1. Create a new Android project in Android Studio.
  2. Make sure you have Kotlin enabled in your project.

Step 2: Create the layout

  1. Open the layout file for your activity (e.g., activity_main.xml).
  2. Add a ListView element to the layout.
    <ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Step 3: Create the model classes

  1. Create a new Kotlin file for the model classes (e.g., Item.kt).
  2. Define the Item class with properties for the item name and a list of sub-items.
    data class Item(val name: String, val subItems: List<String>)

Step 4: Create the adapter

  1. Create a new Kotlin file for the adapter (e.g., ExpandableListAdapter.kt).

  2. Define the ExpandableListAdapter class and extend the BaseExpandableListAdapter class.

  3. Implement the required methods for the adapter.

    class ExpandableListAdapter(private val context: Context, private val items: List<Item>) :
    BaseExpandableListAdapter() {

    override fun getGroupCount(): Int {
    return items.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
    return items[groupPosition].subItems.size
    }

    override fun getGroup(groupPosition: Int): Any {
    return items[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
    return items[groupPosition].subItems[childPosition]
    }

    override fun getGroupId(groupPosition: Int): Long {
    return groupPosition.toLong()
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
    return childPosition.toLong()
    }

    override fun hasStableIds(): Boolean {
    return false
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
    return true
    }

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup): View {
    var view = convertView
    if (view == null) {
    val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    view = inflater.inflate(R.layout.list_group, null)
    }

    val item = getGroup(groupPosition) as Item
    val nameTextView = view!!.findViewById<TextView>(R.id.nameTextView)
    nameTextView.text = item.name

    return view
    }

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup): View {
    var view = convertView
    if (view == null) {
    val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    view = inflater.inflate(R.layout.list_item, null)
    }

    val subItem = getChild(groupPosition, childPosition) as String
    val nameTextView = view!!.findViewById<TextView>(R.id.nameTextView)
    nameTextView.text = subItem

    return view
    }
    }

Step 5: Create the layout for the list items

  1. Create a new layout file for the group item (e.g., list_group.xml).
  2. Add a TextView element to the layout to display the group item name.
    <TextView
    android:id="@+id/nameTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:padding="10dp" />
  3. Create a new layout file for the child item (e.g., list_item.xml).
  4. Add a TextView element to the layout to display the child item name.

Step 6: Set up the activity

  1. Open the Kotlin file for your activity (e.g., MainActivity.kt).
  2. Declare a variable for the ListView and the adapter.
    private lateinit var listView: ListView
    private lateinit var adapter: ExpandableListAdapter
  3. Initialize the ListView and the adapter in the onCreate method.
    listView = findViewById(R.id.listView)
    adapter = ExpandableListAdapter(this, getItems())
    listView.setAdapter(adapter)
  4. Create a helper method getItems to populate the list of items.
    private fun getItems(): List<Item> {
    val items = mutableListOf<Item>()
    items.add(Item("Group 1", listOf("Item 1", "Item 2", "Item 3")))
    items.add(Item("Group 2", listOf("Item 4", "Item 5", "Item 6")))
    items.add(Item("Group 3", listOf("Item 7", "Item 8", "Item 9")))
    return items
    }

Step 7: Run the app

  1. Build and run the app on an Android device or emulator.
  2. You should see a ListView with expandable items.
  3. Click on a group item to expand or collapse it and see the child items.

That's it! You have successfully implemented an expandable ListView in Kotlin for Android. You can customize the layout and adapter as per your requirements.