1

I have a SearchView in the main content area of my activity.

I need to override the SearchView style in my style.xml files (so I can remove the SearchView's underline - as shown here).

However, when I override the SearchView style, its searchIcon just disappears from the UI:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="searchViewStyle">@style/MySearchView</item>
    <item name="android:searchViewStyle">@style/MySearchView</item>
</style>

<style name="MySearchView" parent="Widget.AppCompat.Light.SearchView">
    <!-- nothing entered here yet! -->
</style>

I know I can specify my own drawable for the searchIcon, but I would like to use the default one.

So how do I specify MySearchView style without losing the default searchIcon?

Update #1

I don't know if this is normal, but I also have a problem where if I override the Button style using this approach, then my buttons lose all their padding and therefore become too small:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="buttonStyle">@style/MyButton</item>
    <item name="android:buttonStyle">@style/MyButton</item>
</style>

<style name="MyButton" parent="android:style/Widget.Button">
    <item name="android:textColor">@color/white</item>
    <item name="android:background">@color/red</item>
</style>
Community
  • 1
  • 1
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • 1
    You sure it's not just blending in with the background? – Mike M. Feb 24 '17 at 21:44
  • Yes, well, pretty sure. I've tried making the parent `Widget.AppCompat.Light.SearchView` and then `Widget.AppCompat.SearchView`. I've also temporarily given my activity a red background (rather than white) and there is still no sign of the searchIcon. – ban-geoengineering Feb 24 '17 at 22:58
  • Hmm, not sure then. I couldn't reproduce the issue. Have you tried just specifying the default icon in your style, see what happens? It's `@drawable/abc_ic_search_api_material` (might be `abc_ic_search_api_mtrl_alpha`, depending on your support library version). – Mike M. Feb 24 '17 at 23:03
  • I can specify an icon with `@drawable/ic_search_black_24dp`, but I'd like to get to the bottom of this issue. NB - The "do search" button has gone, too. See **Update #1** in my question as it seems all parent styles are lost for other views that I override. – ban-geoengineering Feb 24 '17 at 23:27
  • 1
    Well, the `Button` issue is probably because you've set a color as the `background`. `Button`s use a specific background drawable to get their default look, and if you're wrapping the width/height, when that drawable's gone, there's nothing to wrap to but the text. You are using the appcompat `SearchView`, yeah? – Mike M. Feb 24 '17 at 23:40
  • 1
    "You are using the appcompat SearchView, yeah?"... "No." :-/ Sorted now - thank you. Do you want to post that as an answer so I can accept it. – ban-geoengineering Feb 25 '17 at 01:38

1 Answers1

1

It seems the issue here was using a framework class with a style meant for a support class. The correct SearchView class to use with an AppCompat theme is android.support.v7.widget.SearchView, rather than android.widget.SearchView.

A framework View isn't going to be looking for attributes defined for a support View, so those attributes' settings will just be lost. Since no valid resources would be found during the SearchView's construction, null would be set as the image for each icon's ImageButton, which would explain why multiple icons seemed to have disappeared with this style.

Mike M.
  • 38,532
  • 8
  • 99
  • 95