Search code examples
androidsearchview

Change the background colour and the query-background colour in SearchView fails


I'm trying to change the colour of my SearchView, how ever when I try following xml code

<SearchView
                android:id="@+id/search_view_id"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:queryHint="Search"
                android:iconifiedByDefault="false"
                android:layout_gravity="center"
                android:background="@color/colorPrimary"
                android:queryBackground="#FFF"/>

I get an error that states:

Error:error: '#FFF' is incompatible with attribute android:queryBackground (attr) reference.

What am I doing wrong here?


Solution

  • Seems like android:queryBackground only accept a resource ID. Just use a color resource instead of hardcoded hex. Keep this in mind.

    Attribute queryBackground is only used in API level 21

     <color name="white">#FFFFFF</color>
    
     <SearchView
        android:id="@+id/search_view_id"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:queryHint="Search"
        android:iconifiedByDefault="false"
        android:layout_gravity="center"
        android:background="@color/colorPrimary"
        android:queryBackground="@color/white"/>