Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

android - Expanding searchView Automatically in Kotlin

I have a searchView in my IndexActivity. On clicking the searchView a new activity is started called SearchActivity. Inside the search activity again, I have the searchView which expands on clicking. It has a query listener which does an external API call.

However, after I open the SearchActivity by clicking on searchView icon, I have to again click on searchView to type in it. What I want is searchView should be focussed as soon as the activity starts. This is my code of searchactivity:

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar_search, menu)
        val searchView: SearchView =
            menu?.findItem(R.id.menu_search)?.actionView as SearchView
        // searchView.setIconifiedByDefault(false)
        // searchView.focusable = View.FOCUSABLE
        // searchView.isIconified = false
        // searchView.requestFocusFromTouch()
        // searchView.onActionViewExpanded()

The commented lines are the ones I tried by searching on the web but it doesn't seem to work.

This is my code in toolbar_searchbar.xml which is included in activity_search set by SearchActivity:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:titleTextColor="@color/cardview_light_background"
            app:subtitleTextAppearance="@font/alegreya_sans_medium"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:background="@android:color/transparent"
            android:id="@+id/toolbar_searchbar"
            app:popupTheme="@style/AppTheme.PopupOverlay">
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

This is the code of toolbar_search.xml which is inflated as seen in the code provided above:

<?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:title="Search"
        android:id="@+id/menu_search"
        android:icon="@drawable/quantum_ic_search_grey600_24"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        />

</menu>

This is what I get at the start.

This is what I get at the start of activity

This is what I want at the start.

This is what I want

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make sure to import: androidx.appcompat.widget.SearchView

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search_contacts"
        android:title="search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="androidx.appcompat.widget.SearchView" >
    </item>
</menu>
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.search_view, menu)
    val searchView: SearchView =
        menu?.findItem(R.id.search_contacts)?.actionView as SearchView

    searchView.isIconified = false
    return true
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...