Is it possible to change the textcolor on an Android SearchView?
The SearchView element doesn’t have any properties for changing the text color. The default text color is black and doesn’t work on our dark background. Is there a way to change the color of the text without resorting to hacks?
I found this similar question related to changing the text size, but so far, it doesn’t have any answers: How to set SearchView TextSize?
Add
<item name="android:editTextColor">@android:color/white</item>
to the parent theme and that should change the entered text. You can also use
<item name="android:textColorHint">@android:color/white</item>
to change the hint text for the SearchView. (Note that you can replace the @android:color/white
with whatever appropriate value you’re hoping to use)
Try something like this : You would get a handle to the textview from the sdk and then change it since they don’t expose it publicly.
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null); TextView textView = (TextView) searchView.findViewById(id); textView.setTextColor(Color.WHITE);
This works for me.
SearchView searchView = (SearchView) findViewById(R.id.search); EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); searchEditText.setTextColor(getResources().getColor(R.color.white)); searchEditText.setHintTextColor(getResources().getColor(R.color.white));
I wanted to do something similar. I finally had to find the TextView
among the SearchView
children:
for (TextView textView : findChildrenByClass(searchView, TextView.class)) { textView.setTextColor(Color.WHITE); }
If you want the util method:
public static <V extends View> Collection<V> findChildrenByClass(ViewGroup viewGroup, Class<V> clazz) { return gatherChildrenByClass(viewGroup, clazz, new ArrayList<V>()); } private static <V extends View> Collection<V> gatherChildrenByClass(ViewGroup viewGroup, Class<V> clazz, Collection<V> childrenFound) { for (int i = 0; i < viewGroup.getChildCount(); i++) { final View child = viewGroup.getChildAt(i); if (clazz.isAssignableFrom(child.getClass())) { childrenFound.add((V)child); } if (child instanceof ViewGroup) { gatherChildrenByClass((ViewGroup) child, clazz, childrenFound); } } return childrenFound; }
Thanks to @CzarMatt I added AndroidX support.
For me the following works. I used a code from a link: Change text color of search hint in actionbar using support library.
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); EditText txtSearch = ((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)); txtSearch.setHint(getResources().getString(R.string.search_hint)); txtSearch.setHintTextColor(Color.LTGRAY); txtSearch.setTextColor(Color.WHITE);
Changing action bar searchview hint text color advices another solution. It works but sets only hint text and color.
searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getResources().getString(R.string.search_hint) + "</font>"));
This is best achieved through custom styles. Overload the action bar widget style with your own custom style. For holo light with dark action bar, put this in your own styles file such as res/values/styles_mytheme.xml
:
<style name="Theme.MyTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar"> <item name="android:actionBarWidgetTheme">@style/Theme.MyTheme.Widget</item> <!-- your other custom styles --> </style> <style name="Theme.MyTheme.Widget" parent="@android:style/Theme.Holo"> <item name="android:textColorHint">@android:color/white</item> <!-- your other custom widget styles --> </style>
Make sure your application is using theme custom theme as described in enter link description here
You can do so by setting the editTextColor
attribute in the style.
<style name="SearchViewStyle" parent="Some.Relevant.Parent"> <item name="android:editTextColor">@color/some_color</item> </style>
and you apply this style to Toolbar
or the SearchView
in the layout.
<android.support.v7.widget.Toolbar android:theme="@style/SearchViewStyle"> <android.support.v7.widget.SearchView /> </android.support.v7.widget.Toolbar>
if you use – android.support.v7.widget.SearchView
SearchView searchView = (SearchView) item.getActionView(); EditText editText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); editText.setTextColor(Color.WHITE);
in Kotlin
val searchView = SearchView(this) val editText = searchView.findViewById<EditText>(androidx.appcompat.R.id.search_src_text) editText.setTextColor(Color.WHITE)
You should rather start using androidx support libraries now
Create a Style for your Toolbar
<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.ActionBar"> <item name="android:editTextColor">@color/some_color</item> </style>
And set it as the theme for the Toolbar
<android.support.v7.widget.Toolbar android:theme="@style/AppTheme.Toolbar" ...
If you’re using the android.support.v7.widget.SearchView, it’s possible without having to use reflection.
Here’s how I’m doing it in my app:
EditText text = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); ImageView searchCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn); View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate); if (searchPlate != null) { searchPlate.setBackgroundResource(R.drawable.search_background); } if (text != null){ text.setTextColor(resources.getColor(R.color.white)); text.setHintTextColor(getResources().getColor(R.color.white)); SpannableStringBuilder magHint = new SpannableStringBuilder(" "); magHint.append(resources.getString(R.string.search)); Drawable searchIcon = getResources().getDrawable(R.drawable.ic_action_view_search); int textSize = (int) (text.getTextSize() * 1.5); searchIcon.setBounds(0, 0, textSize, textSize); magHint.setSpan(new ImageSpan(searchIcon), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Set the new hint text text.setHint(magHint); } if (searchCloseIcon != null){ searchCloseIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_close)); }
They don’t expose the ids publicly for the non appcompat SearchView, but they do for AppCompat if you know where to look. 🙂