Skip to content Skip to sidebar Skip to footer

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 e

Solution 1:

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

<menuxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><itemandroid: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>
overridefunonCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.search_view, menu)
    val searchView: SearchView =
        menu?.findItem(R.id.search_contacts)?.actionView as SearchView

    searchView.isIconified = falsereturntrue
}

Solution 2:

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

Post a Comment for "Expanding Searchview Automatically In Kotlin"