In case SearchView is made by menu configuration, I expand the action view using menu item.
private var searchMenuItem: MenuItem? = null
// create search menu item
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.search_menu, menu)
searchMenuItem = menu?.findItem(R.id.action_search)
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
(searchMenuItem?.actionView as? SearchView)?.apply {
setSearchableInfo(searchManager.getSearchableInfo(componentName))
}
return super.onCreateOptionsMenu(menu)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
setSupportActionBar(findViewById(R.id.toolbar))
// Execute 'expandActionView' delayed
// because I want to focus field only when first start.
Handler().postDelayed({
// show field and focus it.
searchMenuItem?.expandActionView()
}, 500)
}