Search code examples
androidandroid-actionbarsearchviewandroid-appcompat

Icon for SearchView in ActionBar vom AppCompat always has a shadow


I have included a SearchView from AppCompat Actionbar but the icon for it always has a shadow.

For the other icons I am using the icons from the Android Downloads page which don't have a shadow, actually I also tried to assign an icon to the SearchView through android:icon="@drawable/ic_action_search" but that doesn't change the icon.

search icon with shadow

My research showed me that actually the ab_ic_search.png from the android-support-v7-appcompat.jar is being used and my android:icon="@drawable/ic_action_search" is being ignored.

I tried all sorts of approaches like trying to replace the image in onPrepareOptionsMenu (by trying to find an ImageView) or through a custom subclass of the SearchView but to no avail.

Any ideas how to fix that? Basically I'd just like to have my icons uniform, do I have to go the route of adding a shadow to all of the other icons? Probably I won't be able to add a shadow to those "three dots" anyway?


Solution

  • Thanks to this blogpost, I managed to construct my solution:

    public boolean onCreateOptionsMenu(Menu menu) {
        // the search view is in the mymenu.xml
        inflater.inflate(R.menu.mymenu, menu);
    
        // now that it is inflated we can get the item
        MenuItem item = menu.findItem(R.id.action_find);
    
       // and through it the actionview
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        Drawable searchIcon = getActivity().getResources().getDrawable(R.drawable.ic_action_search); // the new icon
    
        // and now we search within the view for the "bad" image
        ImageView collapsedSearchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
    
        // and finally replace it
        collapsedSearchIcon.setImageDrawable(searchIcon);
    }