How to change text color of SearchView?

My SearchView is in the layout (LineaLayout / RelativeLayout), not in the action bar. How to change the color of the text. The default color is white, how to change the color of the text?

+4
source share
4 answers
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));
+13
source

Delving into the appcoppat SearchView source code, I discovered that it uses AppCompatAutoCompleteTextView, which uses the default style autoCompleteTextView.

Creating a different autocomplete style and setting it in the application theme solves the problem for me.

<style name="SearchAutoCompleteTextView" parent="Widget.AppCompat.Light.AutoCompleteTextView">
    <item name="android:textColor">@color/my_text_color</item>
    <item name="android:textColorHint">@color/my_hint_color</item>
</style>

<style name="AppTheme" parent="Theme.AppCompat.Light">
    ...
    <item name="autoCompleteTextViewStyle">@style/SearchAutoCompleteTextView</item>
</style>
+7
source

,

SearchView searchView= (SearchView) findViewById(R.id.searchView1);
int id = searchView.getContext()
               .getResources()
               .getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);

((EditText)  searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
       .setHintTextColor(getResources().getColor(R.color.white));

searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" +    
              getResources().getString(R.string.your_str) + "</font>"));

.

+5

, , .

((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.white));

searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getResources().getString(R.string.hintSearchMess) + "</font>"));
+2

All Articles