简体   繁体   中英

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 want at the start.

这就是我要的

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
}

It would be much easier for you, to have everything on one activity and have only one toolbar. You don't need to create another activity just to start another logic, it shouldn't be bound with the UI.

So your solution is to create a container fragment (if you need a few screens), add a toolbar on top of it and that's all. Move logic to a ViewModel and do all logic there.

By this approach you will solve a few problems:

  • You won't have blinks while screen change
  • Keyboard handling (open/closed)
  • You can easier and more correctly control toolbar and focus for SearchView
  • You don't solve a problem of an incorrect approach but implements everything in better way

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