3

My app has a Light Material Theme, but one section of my activity is dark, and I've placed a SearhView in it. The problem is then that the search icon hint is black-on-blue. Not pretty. So can I override the theme for this single View only?

The intuitive thing to do would be to change the style proerty. So I tried this, to no avail:

<SearchView
        style="@android:style/Widget.Material.SearchView"
        (...)
/>

(the normal style would be Widget.Material.Light.SearchView, I suppose)

Suggestions?

PS: I am not going down this rabbit hole

Community
  • 1
  • 1
Nilzor
  • 18,082
  • 22
  • 100
  • 167

1 Answers1

4

You can set it in your manifest. You can globally define a theme for the whole application but you can apply other themes for activities too.

For Application:

<application
    android:name=".MyApp"
    android:theme="@style/Theme.global">

For Activity:

<activity
      android:name=".SomeActivity"
      android:theme="@style/Theme.search">

UPDATE on comment:

Then set this in your styles.xml where you define your theme attributes like this:

<item name="android:searchViewStyle">@stye/MySearchViewStyle</item>

This is the default for base Searchview:

    <style name="Base.Widget.AppCompat.SearchView" parent="android:Widget">
    <item name="layout">@layout/abc_search_view</item>
    <item name="queryBackground">@drawable/abc_textfield_search_material</item>
    <item name="submitBackground">@drawable/abc_textfield_search_material</item>
    <item name="closeIcon">@drawable/abc_ic_clear_mtrl_alpha</item>
    <item name="searchIcon">@drawable/abc_ic_search_api_mtrl_alpha</item>
    <item name="searchHintIcon">@drawable/abc_ic_search_api_mtrl_alpha</item>
    <item name="goIcon">@drawable/abc_ic_go_search_api_mtrl_alpha</item>
    <item name="voiceIcon">@drawable/abc_ic_voice_search_api_mtrl_alpha</item>
    <item name="commitIcon">@drawable/abc_ic_commit_search_api_mtrl_alpha</item>
    <item name="suggestionRowLayout">@layout/abc_search_dropdown_item_icons_2line</item>
</style>

You can define your style like this:

<style name="MySearchViewStyle" parent="Base.Widget.AppCompat.SearchView">
  <item name="searchIcon">@drawable/my_incredable_search_icon</item>
</style>
Thomas R.
  • 7,988
  • 3
  • 30
  • 39
  • Yea but I don't want to apply it to an entire activity. Just a single view. – Nilzor Oct 08 '15 at 12:41
  • How is "@style/MySearchViewStyle" defined? – Nilzor Oct 08 '15 at 13:46
  • Like every other styles. . – Thomas R. Oct 08 '15 at 13:47
  • What I'm asking is what properties are you setting inside that tag to get a bright search hint for instance (the magnifying glass). A light theme would fix this for me, but if I apply a STYLE only to a single view I need to define those properties myself. I don't know what property governs the color of the magnifying glass (and I've been googling for a while before posting the original post) – Nilzor Oct 08 '15 at 13:49
  • I updated my answer. I posted the base for searchview. There you can see the attributes you can override. Test it out which affects what you are searching for. I never worked with search view :) – Thomas R. Oct 08 '15 at 13:56
  • Thanks a lot, got it working - but not before copying the PNG drawables referenced to my local project. Weirdest thing... – Nilzor Oct 08 '15 at 14:14