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?
Solution 1:
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 color for the SearchView. (Note that you can replace the @android:color/white
with whatever appropriate value you're hoping to use)
Solution 2:
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);
Solution 3:
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));
Solution 4:
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>"));
Solution 5:
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;
}