Search code examples
androidsearchview

Weird gray popup with text when typing in SearchView


I have a SearchView in my app and when I type in it, every character shows up in a weird popup as can be seen below.

Example

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:background="@color/white"
    tools:context="com.example.myapp">

    <SearchView
        android:id="@+id/search_view"
        android:queryHint="@string/select_story_query_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:divider="@color/gray"
        android:dividerHeight="1dp"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/create_new_story_button"
        android:id="@+id/createStoryButton"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:layout_marginBottom="5dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:background="@drawable/low_priority_button_background"
        android:textColor="@color/black" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/done"
        android:id="@+id/select_story_done"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:layout_marginTop="5dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:background="@drawable/button_background"
        android:textColor="@color/white" />
</LinearLayout>

Code:

mSearchView = (SearchView) view.findViewById(R.id.search_view);
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(false);
mSearchView.setQueryHint(getString(R.string.query_hint));

Listener:

@Override
public boolean onQueryTextChange(String newText) {
    if (TextUtils.isEmpty(newText)) {
        mListView.clearTextFilter();
    } else {
        mListView.setFilterText(newText);
    }
    return true;
}

I can't seem to find anything about this issue when I search (guess I am not using the correct keywords). This is also happening on multiple devices running different versions of Android (N5 with 5.1, O+O with 4.4) and only this particular field has this issue, all other EditText fields have no issue.


Solution

  • This has to do with a built in functionality of ListView's filter functionality. As JonasCz pointed out, how to solve this can be found here. My code changes to the onQueryTextChange() method to fix this (basically use the Adapter's filter functionality):

        @Override
        public boolean onQueryTextChange(String newText) {
            Filter filter = adapter.getFilter();
            if (TextUtils.isEmpty(newText)) {
                filter.filter("");
            } else {
                filter.filter(newText);
            }
            return true;
        }