Implementing Search and Display with SearchView and RecyclerView in Kotlin

Project Overview

This guide demonstrates how to build a simple Android application using Kotlin that displays a list of items, allows users to search through the list in real-time, and navigates to a detail screen when an item is clicked. The core components used are RecyclerView for efficient list rendering and SearchView for the search functionality.

Main Activity: Managing the List and Search

The main atcivity is responsible for initializing the data, setting up the RecyclerView, and handling the search input. Instead of recreating the adapter on every search query, a more efficient approach is to update the data source of the existing adapter and notify it of the changes.


class MainActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var searchView: SearchView
    private lateinit var adapter: ItemAdapter

    // Original data source
    private val originalDataList = (1..100).map { "Item number $it" }

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

        setSupportActionBar(findViewById(R.id.toolbar))

        // Initialize RecyclerView
        recyclerView = findViewById(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = ItemAdapter(originalDataList)
        recyclerView.adapter = adapter

        // Initialize SearchView and set up listener
        searchView = findViewById(R.id.searchView)
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                // Filter data based on the new text and update the adapter
                adapter.filter(newText.orEmpty())
                return true
            }
        })
    }
}

Custom Adapter: Binding Data to Views

The adapter is the bridge between the data and the RecyclerView. It holds the data set, creates view holders, and binds data to the views. The filter method updates the internal data list and notifies the RecyclerView to refresh.


class ItemAdapter(private var dataSet: List<string>) : RecyclerView.Adapter<itemadapter.viewholder>() {

    // ViewHolder pattern for efficient view recycling
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val itemTextView: TextView = view.findViewById(R.id.itemTextView)
    }

    // Create new views (invoked by the layout manager)
    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.item_layout, viewGroup, false)
        return ViewHolder(view)
    }

    // Replace the contents of a view (invoked by the layout manager)
    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        viewHolder.itemTextView.text = dataSet[position]

        // Set click listener for navigation
        viewHolder.itemView.setOnClickListener {
            val context = viewHolder.itemView.context
            val intent = Intent(context, DetailActivity::class.java)
            intent.putExtra("message", dataSet[position])
            context.startActivity(intent)
        }
    }

    // Return the size of your dataset (invoked by the layout manager)
    override fun getItemCount() = dataSet.size

    // Filters the data based on a query string
    fun filter(query: String) {
        dataSet = if (query.isEmpty()) {
            originalDataList
        } else {
            originalDataList.filter { it.contains(query, ignoreCase = true) }
        }
        notifyDataSetChanged()
    }
}
</itemadapter.viewholder></string>

View Holder: Optimizing Performance

The view holder pattern is crucial for performence. It caches references to the views within an item layout, preventing the repeatde and expensive calls to findViewById during scrolling.


class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    val itemTextView: TextView = view.findViewById(R.id.itemTextView)
}

Detail Activity: Displaying Item Details

This activity is launched when an item in the list is clicked. It receives the selected item's data via an intent and displays it in a TextView.


class DetailActivity : AppCompatActivity() {

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

        val message = intent.getStringExtra("message")
        val detailTextView: TextView = findViewById(R.id.detailTextView)
        detailTextView.text = message
    }
}

Tags: kotlin Android RecyclerView SearchView adapter

Posted on Sat, 16 May 2026 17:04:04 +0000 by n14charlie