Skip to main content

How to implement multi-select functionality in a ListView in Kotlin Android

How to implement multi-select functionality in a ListView in Kotlin Android.

Here's a step-by-step tutorial on how to implement multi-select functionality in a ListView in Kotlin Android.

Step 1: Set up the project

Create a new Android project in Kotlin and add the necessary dependencies in your build.gradle file.

Step 2: Add a ListView to your layout

Open the layout file for your activity and add a ListView element. Give it an id so that you can reference it later.

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

Step 3: Create a custom adapter

Create a new Kotlin file for your custom adapter. This adapter will handle the data binding for each item in the ListView.

class CustomAdapter(private val context: Context, private val items: List<String>) : BaseAdapter() {

private val selectedItems = ArrayList<String>()

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

override fun getItem(position: Int): Any {
return items[position]
}

override fun getItemId(position: Int): Long {
return position.toLong()
}

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

val itemText = view.findViewById<TextView>(R.id.itemText)
itemText.text = items[position]

val checkBox = view.findViewById<CheckBox>(R.id.checkBox)
checkBox.isChecked = selectedItems.contains(items[position])

checkBox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
selectedItems.add(items[position])
} else {
selectedItems.remove(items[position])
}
}

return view
}

fun getSelectedItems(): List<String> {
return selectedItems
}
}

Step 4: Create the list item layout

Create a new XML layout file for the list item. This layout will define how each item in the ListView should look.

<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">

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingEnd="8dp" />

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

</LinearLayout>

Step 5: Set the adapter for the ListView

In your activity, set the custom adapter for the ListView and populate it with some data.

class MainActivity : AppCompatActivity() {

private lateinit var listView: ListView
private lateinit var adapter: CustomAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

listView = findViewById(R.id.listView)

val items = listOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")
adapter = CustomAdapter(this, items)

listView.adapter = adapter
}
}

Step 6: Get selected items

If you want to retrieve the selected items from the ListView, you can call the getSelectedItems() method on the adapter.

val selectedItems = adapter.getSelectedItems()

That's it! You have successfully implemented multi-select functionality in a ListView in Kotlin Android. You can customize the layout and adapter according to your requirements.