简体   繁体   中英

Set searchView focused when activity opens android

I have a SearchActivity with a menu that holds a searchView.

<?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/searchMessageMenuItem"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="androidx.appcompat.widget.SearchView" />

</menu>

and inside onCreateOptionsMenu i get the searchView

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.search_message_menu, menu)
        searchView = menu?.findItem(R.id.searchMessageMenuItem)?.actionView as SearchView
        searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (!query.isNullOrEmpty() && validateQuery(query))
                    searchViewModel.searchMessage(query)
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean { return true }
        })
        return super.onCreateOptionsMenu(menu)
    }

So when i click on the seach icon the keyboard comes up and the toolbar sets up for write. Well, i want this functionality to happen as default when the activity starts, and not by clicking the search icon. I have tried searchView.requestFocus() but this does not work!

First thing you need is set the app:showAsAction ="always" in your menu.xml

and on your on onCreateOptionsMenu use searchView.setIconifiedByDefault(false) to keep the searchView expended by default.

This does not open the keyboard, it remains that Created this function to open the keyboard:

private fun setFocused(searchView: SearchView) {
    searchView.requestFocus()
    val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
}

and this is the complete Code:

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.search_message_menu, menu)
    searchView = menu?.findItem(R.id.searchMessageMenuItem)?.actionView as SearchView
    searchView.setIconifiedByDefault(false)
    setFocused(searchView)
    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {
            /*
             * your code here
             */
            return true
        }

        override fun onQueryTextChange(newText: String?): Boolean {
            return true
        }
    })
    return super.onCreateOptionsMenu(menu)
}


private fun setFocused(searchView: SearchView) {
    searchView.requestFocus()
    val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
}

and this is the menu:

<?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/searchMessageMenuItem"
        android:icon="@android:drawable/ic_menu_search"

        android:title="Search"
        app:showAsAction="always"
        app:actionViewClass="androidx.appcompat.widget.SearchView" />

</menu>

UPDATE

For the icon which is always displayed I found this solution which can be useful I tried to hide the icon at one at the menu is created but the icon becomes visible when you click on the searchview (Redrawn) so i forced the icon to be hidden when the search view is redrawn and this my solution:

 searchView.setIconifiedByDefault(false)


 searchView.viewTreeObserver.addOnDrawListener {
            searchView.findViewById<AppCompatImageView>(androidx.appcompat.R.id.search_button).visibility = View.GONE
            searchView.findViewById<AppCompatImageView>(androidx.appcompat.R.id.search_mag_icon).visibility = View.GONE
        }
 setFocused(searchView)

and also if you want te keep the close icon visible when the query is empty you can use:

searchView.findViewById<AppCompatImageView>(androidx.appcompat.R.id.search_close_btn).visibility = View.VISIBLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM