Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
How to create a Expandable listView using Kotlin?
This example demonstrates how to create a Expandable listView using Kotlin.
Step 1 − Create a new project in Android Studio, go to File? New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
Example
Step 3 − Create a new kotlin class CustomExpandableListAdapter.kt and add the following code −
Example
import android.content.Context import android.graphics.Typeface import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.BaseExpandableListAdapter import android.widget.TextView import java.util.HashMap class CustomExpandableListAdapter internal constructor( private val context: Context, private val titleList: List, private val dataList: HashMap > ) : BaseExpandableListAdapter() { override fun getChild(listPosition: Int, expandedListPosition: Int): Any { return this.dataList[this.titleList[listPosition]]!![expandedListPosition] } override fun getChildId(listPosition: Int, expandedListPosition: Int): Long { return expandedListPosition.toLong() } override fun getChildView( listPosition: Int, expandedListPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup ): View { var convertView = convertView val expandedListText = getChild(listPosition, expandedListPosition) as String if (convertView == null) { val layoutInflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater convertView = layoutInflater.inflate(R.layout.list_item, null) } val expandedListTextView = convertView!!.findViewById (R.id.listView) expandedListTextView.text = expandedListText return convertView } override fun getChildrenCount(listPosition: Int): Int { return this.dataList[this.titleList[listPosition]]!!.size } override fun getGroup(listPosition: Int): Any { return this.titleList[listPosition] } override fun getGroupCount(): Int { return this.titleList.size } override fun getGroupId(listPosition: Int): Long { return listPosition.toLong() } override fun getGroupView( listPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup ): View { var convertView = convertView val listTitle = getGroup(listPosition) as String if (convertView == null) { val layoutInflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater convertView = layoutInflater.inflate(R.layout.list_item, null) } val listTitleTextView = convertView!!.findViewById (R.id.listView) istTitleTextView.setTypeface(null, Typeface.BOLD) listTitleTextView.text = listTitle return convertView } override fun hasStableIds(): Boolean { return false } override fun isChildSelectable(listPosition: Int, expandedListPosition: Int): Boolean { return true } }
Step 4 − Create a new class ExpandableListData.kt and add the following code −
Example
import java.util.*
internal object ExpandableListData {
val data: HashMap>
get() {
val expandableListDetail =
HashMap>()
val myFavCricketPlayers: MutableList =
ArrayList()
myFavCricketPlayers.add("MS.Dhoni")
myFavCricketPlayers.add("Sehwag")
myFavCricketPlayers.add("Shane Watson")
myFavCricketPlayers.add("Ricky Ponting")
myFavCricketPlayers.add("Shahid Afridi")
val myFavFootballPlayers: MutableList = ArrayList()
myFavFootballPlayers.add("Cristiano Ronaldo")
myFavFootballPlayers.add("Lionel Messi")
myFavFootballPlayers.add("Gareth Bale")
myFavFootballPlayers.add("Neymar JR")
myFavFootballPlayers.add("David de Gea")
val myFavTennisPlayers: MutableList = ArrayList()
myFavTennisPlayers.add("Roger Federer")
myFavTennisPlayers.add("Rafael Nadal")
myFavTennisPlayers.add("Andy Murray")
myFavTennisPlayers.add("Novak Jokovic")
myFavTennisPlayers.add("Sania Mirza")
expandableListDetail["CRICKET PLAYERS"] = myFavCricketPlayers
expandableListDetail["FOOTBALL PLAYERS"] = myFavFootballPlayers
expandableListDetail["TENNIS PLAYERS"] = myFavTennisPlayers
return expandableListDetail
}
}
Step 5 − Add the following code to src/MainActivity.kt
Example
import android.os.Bundle
import android.widget.ExpandableListAdapter
import android.widget.ExpandableListView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import app.com.q14.ExpandableListData.data
class MainActivity : AppCompatActivity() {
private var expandableListView: ExpandableListView? = null
private var adapter: ExpandableListAdapter? = null
private var titleList: List? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
expandableListView = findViewById(R.id.expendableList)
if (expandableListView != null) {
val listData = data
titleList = ArrayList(listData.keys)
adapter = CustomExpandableListAdapter(this, titleList as ArrayList, listData)
expandableListView!!.setAdapter(adapter)
expandableListView!!.setOnGroupExpandListener { groupPosition ->
Toast.makeText(
applicationContext,
(titleList as ArrayList)[groupPosition] + " List Expanded.",
Toast.LENGTH_SHORT
).show()
}
expandableListView!!.setOnGroupCollapseListener { groupPosition ->
Toast.makeText(
applicationContext,
(titleList as ArrayList)[groupPosition] + " List Collapsed.",
Toast.LENGTH_SHORT
).show()
}
expandableListView!!.setOnChildClickListener { _, _, groupPosition, childPosition, _ ->
Toast.makeText(
applicationContext,
"Clicked: " + (titleList as ArrayList)[groupPosition] + " -> " + listData[(
titleList as
ArrayList
)
[groupPosition]]!!.get(
childPosition
),
Toast.LENGTH_SHORT
).show()
false
}
}
}
}
Step 6 − Create a new Layout resource (list_item.xml) and add the following code.
Example
Step 7 − Add the following code to androidManifest.xml
Example
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run icon
from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen

