Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change the textcolor on an Android SearchView?

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)


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));

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>"));

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;
}

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>