material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.38k stars 3.07k forks source link

[Search Bar] Setting searchBar.setText(...) does not work without android:windowSoftInputMode="adjustNothing" #3360

Open ljubarskij opened 1 year ago

ljubarskij commented 1 year ago

Description: I have copied your code from SearchRecyclerDemoActivity and while playing with search bar I have noticed that doing searchBar.setText(...) inside submitSearchQuery() does nothing (no effect). Once I have configured android:windowSoftInputMode="adjustNothing" in my activity, it started working.

Expected behavior: searchBar.setText(...) should work even without setting windowSoftInputMode. (IDK, I am new to android, but I guess it should not depend on windowSoftInputMode)

Source code:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.library.LibraryFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listitem="@layout/recycler_view_item" />

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.search.SearchBar
            android:id="@+id/search_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Search" />

    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.search.SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="Search"
        app:layout_anchor="@id/search_bar">
    </com.google.android.material.search.SearchView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Android API version: 33

Material Library version: 1.9.0-rc01

Device: xiaomi mi note 10 lite (MIUI Global 12.5.6 Stable)

drchen commented 1 year ago

Hi, thanks for the report. Can you provide more info? By "not working" do you mean it doesn't perform the search or?

ljubarskij commented 1 year ago

Hi @drchen, sorry for confusion. When I call searchBar.setText("text") I expect "text" to be populated into search bar. I understand that to perform a search, I need to implement it myself.