In this article, we shall discuss how to add a click listener to listview in android using kotlin such that you can select an item on a list and use it to open a new activity or know which item has been clicked
The prerequisites for this article are
Having discussed how to create a listview in android using kotlin in the previous article, we shall now focus on how we can implement the click listener to the items on the listview
How to create a listview in android
The content in the activity_main.xml is as follows
<?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">
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
The layout resource file we created to hold the textview for each array item is as follows
<?xml version="1.0" encoding="utf-8"?>
<!-- to display the list in each text view -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listviewvalues"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="15dp"
android:textStyle="bold" >
</TextView>
The MainActivity.kt code which added the array items to the array adapter such that it can be visible in the listview is as follows
package com.example.listviewproject
import android.content.Intent
import android.os.Bundle
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
var carlist = arrayOf("Nissan", "Toyota", "Audi", "VW", "Benz", "KIA", "Suzuki", "Subaru", "Honda", "Mitsubishi");
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listViewlist = findViewById<ListView>(R.id.listview1)
val arrayAdapter = ArrayAdapter<String>(
this,
R.layout.listview_layout, carlist
)
listViewlist.adapter=arrayAdapter
}
}
How to implement onclick listener to listview in android
- Define the onclick listener using the listview as follows
listViewlist.onItemClickListener = AdapterView.OnItemClickListener {
adapterView, view, position, id ->
}
- Next, define the items you want to collect once the listview is clicked either the name displayed in the listview or the position of the item in the listview
Create variables for items as shown
val selectedItem = adapterView.getItemAtPosition(position) as String
val itemposition = adapterView.getItemIdAtPosition(position)
- Next, you can display a toast message to show the item clicked or you can open a new activity and pass the items of the clicked item
To display a toast message for the item clicked and its position, use this code
Toast.makeText(
applicationContext,
"You have clicked $selectedItem at position $itemposition",
Toast.LENGTH_LONG
).show()
To open a new activity, you will need to create the new activity first by right clicking app, followed by new activity then empty activity
Use intent to define the next activity and put extras such that the name of the item can be used in the next activity
val intent = Intent(applicationContext, MainActivity2::class.java)
intent.putExtra("itemname", selectedItem)
startActivity(intent)
The full onclick listener for listview is as follows
listViewlist.onItemClickListener = AdapterView.OnItemClickListener {
adapterView, view, position, id ->
val selectedItem = adapterView.getItemAtPosition(position) as String
val itemposition = adapterView.getItemIdAtPosition(position)
//to display a message once the item in list is clicked
Toast.makeText(
applicationContext,
"You have clicked $selectedItem at position $itemposition",
Toast.LENGTH_LONG
).show()
//to open a new activity when item in a list is clicked
val intent = Intent(applicationContext, MainActivity2::class.java)
intent.putExtra("itemname", selectedItem)
startActivity(intent)
}
The full code for MainActivity.kt that has the click listener for listview is as follows
package com.example.listviewproject
import android.content.Intent
import android.os.Bundle
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
var carlist = arrayOf("Nissan", "Toyota", "Audi", "VW", "Benz", "KIA", "Suzuki", "Subaru", "Honda", "Mitsubishi");
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listViewlist = findViewById<ListView>(R.id.listview1)
val arrayAdapter = ArrayAdapter<String>(
this,
R.layout.listview_layout, carlist
)
listViewlist.adapter=arrayAdapter
listViewlist.onItemClickListener = AdapterView.OnItemClickListener {
adapterView, view, position, id ->
val selectedItem = adapterView.getItemAtPosition(position) as String
val itemposition = adapterView.getItemIdAtPosition(position)
//to display a message once the item in list is clicked
Toast.makeText(
applicationContext,
"You have clicked $selectedItem at position $itemposition",
Toast.LENGTH_LONG
).show()
//to open a new activity when item in a list is clicked
val intent = Intent(applicationContext, MainActivity2::class.java)
intent.putExtra("itemname", selectedItem)
startActivity(intent)
}
}
}
In the next activity that is opened after clicking an item in the listview, you will need to use getextras method so that you can display results depending on the specific item that is clicked
The code for the other activity is as follows
package com.example.listviewproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
class MainActivity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
val bundle: Bundle? = intent.extras
val itemname: String? = bundle?.getString("itemname")
Toast.makeText(applicationContext,itemname,Toast.LENGTH_LONG).show()
}
}