20

My SearchView is located in a LinearLayout / RelativeLayout rather than the action bar.

Its default text color is white, how to change it?

finnmglas
  • 1,626
  • 4
  • 22
  • 37
jojo
  • 463
  • 2
  • 5
  • 17

9 Answers9

47

Digging into the source code of the appcompat SearchView I found that it uses an AppCompatAutoCompleteTextView which by default uses the autoCompleteTextView style.

Creating a different autocomplete style and setting it in the app theme solved 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>
Corneliu Dascălu
  • 3,900
  • 1
  • 28
  • 38
  • This should be the correct answer - if you want to change the text/hint color for a android.support.v7.widget.SearchView – DiscDev Nov 14 '17 at 12:34
  • 10
    This works, and you can set it for individual searchViews by adding xml ```app:theme="@style/SearchViewTextTheme"``` instead of applying it to the whole ```AppTheme``` – Carson Holzheimer Mar 14 '18 at 05:12
  • You're the MVP, none of the other solutions worked, but this one does perfectly – FabioR Sep 23 '22 at 22:03
36

For Androidx Library

SearchView searchView = (SearchView) findViewById(R.id.search);
EditText searchEditText = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.white));
searchEditText.setHintTextColor(getResources().getColor(R.color.white));

For Android Support Library

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));
Priyankchoudhary
  • 786
  • 9
  • 12
Android Geek
  • 8,956
  • 2
  • 21
  • 35
10

Try this,

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

or

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

or

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

This may helps you.

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
7

For me worked next approach:

1.add style into your styles.xml

<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
    <item name="android:textColor">@color/white</item>
    <item name="android:editTextColor">@color/white</item>
    <item name="android:textColorHint">@color/light_gray</item>
</style>

2.add this line in your SearchView layout code

app:theme="@style/SearchViewStyle"
hornet2319
  • 379
  • 3
  • 15
5

I would do this kind of job through the xml layout file. This can be found in the res/layout folder.

You would need to do something similar to the following:

Add this to the parent theme.

<item name="android:editTextColor">@android:color/white</item>

This should change the entered text.

You can also use something like this:

<item name="android:textColorHint">@android:color/white</item>

It will change the hint text for the SearchView.

Hope this helps :)

Sajjad Asaad
  • 71
  • 1
  • 4
3

Just override the default color using the android:theme attribute:

   <androidx.appcompat.widget.SearchView
       android:theme="@style/ThemeOverlay.SearchView"

with

   <style name="ThemeOverlay.SearchView" parent="">
        <!-- Text color -->
        <item name="android:editTextColor">@color/...</item>
        <!-- Hint text color -->
        <item name="android:textColorHint">@color/...</item>
    </style>

enter image description here enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
2

Try this, if its not working try to change your theme.

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

or try like this

searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getResources().getString(R.string.hintSearchMess) + "</font>"));
Jithu P.S
  • 1,843
  • 1
  • 19
  • 32
0

In this link colors of search icon, search hint icon, close icon, plate, query hint color, query color are changed

nawaab saab
  • 1,892
  • 2
  • 20
  • 36
0

This worked for me:

EditText searchEditText = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.white));
searchEditText.setHint("Search");
searchEditText.setHintTextColor(getResources().getColor(R.color.white));
Franco
  • 669
  • 2
  • 8
  • 23