Skip to content

Instantly share code, notes, and snippets.

@japharr
Created March 16, 2020 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japharr/5961f85a4cd1dad6408d3953597a45a1 to your computer and use it in GitHub Desktop.
Save japharr/5961f85a4cd1dad6408d3953597a45a1 to your computer and use it in GitHub Desktop.
Creating Android Native SearchView in ActionBar
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val searchItem: MenuItem? = menu.findItem(R.id.action_search)
if (searchItem != null) {
searchView = MenuItemCompat.getActionView(searchItem) as SearchView
searchView.setOnCloseListener {
searchView.onActionViewCollapsed()
Log.v("MainActivity", "setOnCloseListener: onClose")
true
}
searchView.setOnFocusChangeListener { v, hasFocus ->
Log.v("MainActivity", "setOnFocusChangeListener: hasFocus: $hasFocus")
}
val searchPlate = searchView.findViewById(androidx.appcompat.R.id.search_src_text) as EditText
searchPlate.hint = "Search"
val searchPlateView: View =
searchView.findViewById(androidx.appcompat.R.id.search_plate)
searchPlateView.setBackgroundColor(
ContextCompat.getColor(
this,
android.R.color.transparent
)
)
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
Toast.makeText(applicationContext, query, Toast.LENGTH_SHORT).show()
this@MainActivity.query = query
mMenuItemChangedListener?.onDrawerItemClickListener(selectedIdentifier, query)
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
return false
}
})
val searchManager =
getSystemService(Context.SEARCH_SERVICE) as SearchManager
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment