I'm trying to change the drawable that sits in the Android actionbar searchview widget.
Currently it looks like this:
but I need to change the blue background drawable to a red colour.
I've tried many things short of rolling my own search widget, but nothing seems to work.
Can somebody point me in the right direction to changing this?
Intro
Unfortunately there's no way to set
SearchView
text field style using themes, styles and inheritance in XML as you can do with background of items in ActionBar dropdown. This is becauseselectableItemBackground
is listed as styleable inR.stylable
, whereassearchViewTextField
(theme attribute that we're interested in) is not. Thus, we cannot access it easily from within XML resources (you'll get aNo resource found that matches the given name: attr 'android:searchViewTextField'
error).Setting SearchView text field background from code
So, the only way to properly substitute background of
SearchView
text field is to get into it's internals, acquire access to view that has background set based onsearchViewTextField
and set our own.NOTE: Solution below depends only on id (
android:id/search_plate
) of element withinSearchView
, so it's more SDK-version independent than children traversal (e.g. usingsearchView.getChildAt(0)
to get to the right view withinSearchView
), but it's not bullet-proof. Especially if some manufacturer decides to reimplement internals ofSearchView
and element with above-mentioned id is not present - the code won't work.In SDK, the background for text field in
SearchView
is declared through nine-patches, so we'll do it the same way. You can find original png images in drawable-mdpi directory of Android git repository. We're interested in two image. One for state when text field is selected (named textfield_search_selected_holo_light.9.png) and one for where it's not (named textfield_search_default_holo_light.9.png).Unfortunately, you'll have to create local copies of both images, even if you want to customize only focused state. This is because
textfield_search_default_holo_light
is not present in R.drawable. Thus it's not easily accessible through@android:drawable/textfield_search_default_holo_light
, which could be used in selector shown below, instead of referencing local drawable.NOTE: I was using Holo Light theme as base, but you can do the same with Holo Dark. It seems that there's no real difference in selected state 9-patches between Light and Dark themes. However, there's a difference in 9-patches for default state (see Light vs Dark). So, probably there's no need to make local copies of 9-patches for selected state, for both Dark and Light themes (assuming that you want to handle both, and make them both look the same as in Holo Theme). Simply make one local copy and use it in selector drawable for both themes.
Now, you'll need to edit downloaded nine-patches to your need (i.e. changing blue color to red one). You can take a look at file using draw 9-patch tool to check if it is correctly defined after your edit.
I've edited files using GIMP with one-pixel pencil tool (pretty easy) but you'll probably use the tool of your own. Here's my customized 9-patch for focused state:
NOTE: For simplicity, I've used only images for mdpi density. You'll have to create 9-patches for multiple screen densities if, you want the best result on any device. Images for Holo
SearchView
can be found in mdpi, hdpi and xhdpi drawable.Now, we'll need to create drawable selector, so that proper image is displayed based on view state. Create file
res/drawable/texfield_searchview_holo_light.xml
with following content:We'll use the above created drawable to set background for
LinearLayout
view that holds text field withinSearchView
- its id isandroid:id/search_plate
. So here's how to do this quickly in code, when creating options menu:Final effect
Here's the screenshot of the final result:
How I got to this
I think it's worth metioning how I got to this, so that this approach can be used when customizing other views.
Checking out view layout
I've checked how
SearchView
layout looks like. In SearchView contructor one can find a line that inflates layout:Now we know that
SearchView
layout is in file namedres/layout/search_view.xml
. Looking into search_view.xml we can find an innerLinearLayout
element (with idsearch_plate
) that hasandroid.widget.SearchView$SearchAutoComplete
inside it (looks like ours search view text field):Now, we now that the background is set based on current theme's
searchViewTextField
attribute.Investigating attribute (is it easily settable?)
To check how
searchViewTextField
attribute is set, we investigate res/values/themes.xml. There's a group of attributes related toSearchView
in defaultTheme
:We see that for default theme the value is
@drawable/textfield_searchview_holo_dark
. ForTheme.Light
value is also set in that file.Now, it would be great if this attribute was accessible through R.styleable, but, unfortunately it's not. For comparison, see other theme attributes which are present both in themes.xml and R.attr like textAppearance or selectableItemBackground. If
searchViewTextField
was present inR.attr
(andR.stylable
) we could simply use our drawable selector when defining theme for our whole application in XML. For example:What should be modified?
Now we know, that we'll have to access
search_plate
through code. However, we still don't know how it should look like. In short, we search for drawables used as values in default themes: textfield_searchview_holo_dark.xml and textfield_searchview_holo_light.xml. Looking at content we see that the drawable isselector
which reference two other drawables (which occur to be 9-patches later on) based on view state. You can find aggregated 9-patch drawables from (almost) all version of Android on androiddrawables.comCustomizing
We recognize the blue line in one of the 9-patches, so we create local copy of it and change colors as desired.