I have a field where the user can type a search query in the action bar of the application. This is declared in the action bar using a menu inflate in the Activity:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+id/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"
android:title="@string/search"
></item>
</menu>
I need to customize the appearance of the SearchView (for instance background and text color). So far I could not find a way to do it using XML (using styles or themes).
Is my only option to do it in the code when inflating the menu?
Edit #1: I have tried programmatically but I cannot get a simple way to set the text color. Plus when I do searchView.setBackgroundResource(...) The background is set on the global widget, (also when the SearchView is iconified).
Edit #2: Not much information on the Search Developer Reference either
How do you inflate the menu xml in your Activity? if you inflate the menu by using getMenuInflator() in your Activity, then the menu and also the searchView get the themed context, that have attached to the activity.
if you check the source code of Activity.getMenuInflator() at API-15, you can see the themed context codes. Here it is.
adding my take on things which is probably a little more efficient and safe across different android versions.
you can actually get a numeric ID value from a string ID name. using android's
hierarchyviewer
tool, you can actually find the string IDs of the things you are interested in, and then just usefindViewById(...)
to look them up.the code below sets the hint and text color for the edit field itself. you could apply the same pattern for other aspects that you wish to style.
In case anyone wants to modify the views directly, here is how you can change the colors/fonts/images and customize the search box to your pleasure. It is wrapped in a try/catch in case there are differences between versions or distributions, so it won't crash the app if this fails.
This example shows changing the text color and the background colors of the cancel/accept images. searchView is a SearchView object already instantiated with it's background color:
Here is the drawable code:
Obviously, this is hacky, but it will work for now.
From ICS this is doable using themes and styles. I'm using ActionBarSherlock which makes it applicable also for HC and below.
Add a style to define "android:textColorHint":
Apply this as "actionBarWidgetTheme" to your theme:
Presto! Make sure that you use
getSupportActionBar().getThemedContext()
(orgetSupportActionBar()
for ActionBarSherlock) if any widgets are initiated where you might have other themes in effect.You can use the attribute
android:actionLayout
instead which lets you specify a layout to be inflated. Just have a layout with yourSearchView
and you won't have to modify anything really.As to changing text style on the
SearchView
that is probably not possible as theSearchView
is aViewGroup
. You should probably try changing text color via themes instead.Seibelj had an answer that is good if you want to change the icons. But you'll need to do it for every API version. I was using ICS with ActionBarSherlock and it didn't do justice for me but it did push me in the correct direction.
Below I change the text color and hint color. I showed how you might go about changing the icons too, though I have no interest in that for now (and you probably want to use the default icons anyways to be consistent)