how to change search icon, hint and text color in Android SearchView?

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>

1 answer

  • answered 2021-03-08 06:02 Shailendra Madda

    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