jojo
jojo

Reputation: 473

How to change SearchView textcolor?

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

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

Upvotes: 21

Views: 26051

Answers (10)

FireLord
FireLord

Reputation: 1

This worked for me on latest sdk34.

in the theme.xml

    <style name="ThemeSearchView" parent="TextAppearance.Material3.SearchView">
         <item name="android:editTextColor">@color/black</item>
         <item name="android:textColorHint">@color/secondary</item>
     </style>

and in your searchView layout

app:theme="@style/ThemeSearchView"

Upvotes: 0

Lahar Naru
Lahar Naru

Reputation: 1

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

Upvotes: 0

Android Geek
Android Geek

Reputation: 9225

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

Upvotes: 38

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363687

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

Upvotes: 4

nawaab saab
nawaab saab

Reputation: 1902

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

Upvotes: 0

hornet2319
hornet2319

Reputation: 339

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"

Upvotes: 8

Sajjad Asaad
Sajjad Asaad

Reputation: 71

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

Upvotes: 5

Corneliu Dascălu
Corneliu Dascălu

Reputation: 3958

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>

Upvotes: 47

Sathish Kumar J
Sathish Kumar J

Reputation: 4335

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.

Upvotes: 10

Jithu P.S
Jithu P.S

Reputation: 1843

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

Upvotes: 2

Related Questions