How do we set Android Searchview on Toolbar

Last updated Jun 26, 2021


In this android example we will learn how to add Serchview widget in toolbar. We know searchview widget provides the user interface to enter search key and submit to search the data based on key. 

In this example we will takes an activity and will ass searchview widget on the toolbar by adding menu item.

 

How to add ndroid Searchview inside Toolbar

 

Let's get started

 

Step 1: Create android application in android studio

Step 2: Create a menu layout with searchview widget as menu item. This menu layout will be like below

xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/app_bar_search"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:showAsAction="ifRoom|withText"

        app:actionViewClass="android.widget.SearchView"/>
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
android:icon="@drawable/ic_filter"
        app:showAsAction="ifRoom" />
menu>

 

 

Step 3: Read menu layout inside activity by implementing override fun onCreateOptionsMenu(menu: Menu)

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu_sales, menu)

return true

}

 

Step 4: Read searchview widget and handle search functionality on the submit

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu_sales, menu)
    val searchViewItem: MenuItem = menu!!.findItem(R.id.app_bar_search)
    val searchView: SearchView = MenuItemCompat.getActionView(searchViewItem) as SearchView
searchView.col
    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {
            searchView.clearFocus()
            Toast.makeText(applicationContext,"Search Key : $query ",Toast.LENGTH_LONG).show()

            return false
        }

        override fun onQueryTextChange(newText: String?): Boolean {

            return false
        }
    })
    return true
}

 

in the above code will read menu item as searchview widget and handle the search key

 

Complete activity code will be like this

package com.rrtutors.highvalue.dashboard.ui.sales

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.SearchView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.MenuItemCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.rrtutors.highvalue.R
import com.rrtutors.highvalue.dashboard.ui.sales.data.Actions
import com.rrtutors.highvalue.dashboard.ui.sales.data.SalesModel
import kotlinx.android.synthetic.main.activity_sales_list.*


class SalesListActivity : AppCompatActivity() {
    lateinit var saleList:MutableList<SalesModel>
    lateinit var salesViewAdapter: SalesViewAdapter
    lateinit var obj: ActivityResultLauncher<Intent>;
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sales_list)
        toolbar_sales.setTitle("")
        setSupportActionBar(toolbar_sales)
        
    }
    

    

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_sales, menu)
        val searchViewItem: MenuItem = menu!!.findItem(R.id.app_bar_search)
        val searchView: SearchView = MenuItemCompat.getActionView(searchViewItem) as SearchView

        val id = searchView.context.resources
            .getIdentifier("android:id/search_src_text", null, null)
        val textView = searchView.findViewById<View>(id) as TextView
        textView.setTextColor(Color.WHITE)
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                searchView.clearFocus()
                Toast.makeText(applicationContext,"Search Key : $query ",Toast.LENGTH_LONG).show()

                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {

                return false
            }
        })
        return true
    }
}

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

52 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions