0

So I was working with this SearchView and everything looked fine in the emulator. when I tested it in the actual device, all of the icons turned white.

I looked up on some of the sources on how to style the SearchView but most of them are maybe outdated and nothing seems to work.

Current SearchView XML:

<SearchView
            android:id="@+id/lead_search_view"
            style="@style/AppTheme.SearchView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/custom_background_spinner"
            android:iconifiedByDefault="false"
            android:queryBackground="@android:color/transparent"
            android:queryHint="Enter a keyword"
            android:theme="@style/AppTheme.SearchView" />

The style that I followed on some solutions:

 <style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:textColorHint">@color/colorHint</item>
        <item name="searchIcon">@drawable/ic_search_field_icon</item>
    </style>
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
nerdy kid
  • 371
  • 1
  • 3
  • 14

2 Answers2

2

Use this way to change the search icon:

private SearchView searchbox;

private void customizeSearchbox() {
    setSearchHintIcon(R.drawable.new_search_icon);
}

private void setSearchHintIcon(int resourceId) {
    ImageView searchHintIcon = (ImageView) findViewById(searchbox, 
        "android:id/search_mag_icon");
    searchHintIcon.setImageResource(resourceId);
}

private View findViewById(View v, String id) {
    return v.findViewById(v.getContext().getResources().
        getIdentifier(id, null, null));        
}

as stated here: https://stackoverflow.com/a/16988719/2462531

To change text and hint color try this:

Add this to the parent theme.

<item name="android:editTextColor">@android:color/white</item>
<item name="android:textColorHint">@android:color/white</item>

It will change the hint text for the SearchView.

as stated here: https://stackoverflow.com/a/39036236/2462531

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • 1
    Pls do not just add link to other answers. This is only qualified as a comment . And if this question is already answered you should just mark it as duplicate .' – ADM Mar 08 '21 at 06:34
  • 1
    Eh it took me a while to find out that the icon I want to change is `android:id/search_mag_icon`. Useful answer, thanks! – Micer Apr 27 '22 at 08:38
  • 1
    @ADM It is answered for one more reason along with the existing answer, also added that link here for reference. – Shailendra Madda Apr 27 '22 at 09:47
0

You can change and customize all SearchView icons like this:

  <androidx.appcompat.widget.SearchView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:iconifiedByDefault="false"
        app:queryHint="@string/search"
        app:searchHintIcon="@drawable/ic_search_gray"
        app:searchIcon="@drawable/ic_search_white"
        app:closeIcon="@drowable/x_icon"/>

these three lines make magic

        app:searchHintIcon="@drawable/ic_search_gray"
        app:searchIcon="@drawable/ic_search_white" 
        app:closeIcon="@drowable/x_icon"/>

but first, you must provide declaration in root layout in your xml with this 2 lines of code :

xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"

I hope this will helps someone, since it's a late reply...

enjoy and happy coding :)