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

39

144

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?

Carthage answered 4/7, 2012 at 0:51 Comment(2)
please mention what you have tried.Revolution
Yes, it is possible. Check this out: #18260207Dogmatism
F
144

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)

Foliate answered 12/11, 2014 at 21:35 Comment(9)
This fixed it for me, and didn't involve any complicated shenanigans using reflection. Also works with appcompat.Clematis
You can also set android:theme on the searchview to a style that has the android:editTextColor element. This way you only affect that searchview.Skylar
It works! I was using a search view inside a toolbar, extended ThemeOverlay.AppCompat.Light and added the above item and set the style on the Toolbar widget ;) Works like a charm...Haldane
This solution doesn't work for me. Text has still the wrong color. The answer below with setting the hint color programmatically worked for me.Smog
You are a genius buddy .. thanks a ton for the great solution.Zook
This will change all edit text color in whole Application. i.e change all the edit text color . not only for Searchview. :(Bookkeeping
Thank You. Use this in a custom theme , apply theme on any activity you want.Cholecystitis
The problem with this answer is that it will also change the text color of your EditTexts. The only solution that has worked for me that changes the SearchView text color alone is here: https://mcmap.net/q/161117/-how-to-change-searchview-textcolorCrappie
I second that the link provided by @Crappie worked for meRelapse
F
105

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);
Furcate answered 16/1, 2013 at 17:29 Comment(5)
This allways returns nullInstrumentalist
Note that if you're using ActionBarSherlock the view IDs will be different. For example, the lib currently sets the edit text ID to "package:id/abs__search_src_text", which can be accessed directly via R.id.abs__search_src_text.Lorraine
Also, what I actually wanted to change was the SearchView hint text color, which is done easily via textView.setHintTextColor().Lorraine
This is a very dirty hack. Use android:editTextColor and android:textColorHint instead like akdotcom suggestedIndopacific
For AndroidX you'll have to use searchView.findViewById<TextView>(androidx.appcompat.R.id.search_src_text) to find the TextViewTutty
A
75

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));
Arreola answered 7/5, 2015 at 4:27 Comment(3)
This also works if you obtain the view as a TextView or even as an AutoCompleteTextView.Nayarit
Yes, this is work for me after following the suggestion of @JuanJoséMeleroGómezDasilva
I suppose, this was copied and shortened from my answer here: https://mcmap.net/q/158634/-is-it-possible-to-change-the-textcolor-on-an-android-searchview.Tunstall
T
36

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>"));
Tunstall answered 8/10, 2014 at 7:18 Comment(1)
Use androidx.appcompat.R.id.search_src_text if you've migrated your project to AndroidX.Lonely
S
27

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;
}
Scabies answered 7/5, 2014 at 15:54 Comment(5)
I too was getting the null exception with previous solutions, and this worked for me!Mesozoic
This should be marked as accepted answer. The above one gives null. Thanks for the good work buddy :)Ninetta
This solution is so disgusting but its the only one that actually works and it is Android team to blame!Create
How would we access the search icon with this ?Hessler
AMAZING ! The best solution ever ...all this xml design is....Tub
P
18

You can override the default color using the android:theme attribute in the View.

If you are using a Toolbar or MaterialToolbar:

<com.google.android.material.appbar.MaterialToolbar
    android:theme="@style/ThemeOverlay.Toobar"
    ...>

or:

<androidx.appcompat.widget.Toolbar
    android:theme="@style/ThemeOverlay.Toobar"
    ...>

where the Theme overlay with a Material Components Theme is:

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

with an AppCompat theme:

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

enter image description here


If you are using a SearchView in your layout you can do something similar:

   <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

Premises answered 18/7, 2020 at 8:31 Comment(0)
T
16

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

Tsaritsyn answered 20/4, 2014 at 16:15 Comment(0)
B
16

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>
Brainless answered 14/10, 2016 at 1:18 Comment(2)
+1 This trumps all solutions involving findViewById imho. View styling should be used whenever possible.Esquimau
This should be the accepted answer! Very clean and easy.Penurious
F
10

if you use - android.support.v7.widget.SearchView

SearchView searchView = (SearchView) item.getActionView();
EditText editText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
editText.setTextColor(Color.WHITE);

in Kotlin

val searchView = SearchView(this)
val editText = searchView.findViewById<EditText>(androidx.appcompat.R.id.search_src_text)
editText.setTextColor(Color.WHITE)

You should rather start using androidx support libraries now

Fuscous answered 9/3, 2017 at 9:36 Comment(0)
V
8

Create a Style for your Toolbar

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="android:editTextColor">@color/some_color</item>
</style>

And set it as the theme for the Toolbar

<android.support.v7.widget.Toolbar
    android:theme="@style/AppTheme.Toolbar"
    ...
Vanwinkle answered 24/7, 2017 at 14:23 Comment(0)
T
6

If you're using the android.support.v7.widget.SearchView, it's possible without having to use reflection.

Here's how I'm doing it in my app:

EditText text = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
ImageView searchCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);

if (searchPlate != null) {
    searchPlate.setBackgroundResource(R.drawable.search_background);
}

if (text != null){
    text.setTextColor(resources.getColor(R.color.white));
    text.setHintTextColor(getResources().getColor(R.color.white));

    SpannableStringBuilder magHint = new SpannableStringBuilder("  ");
    magHint.append(resources.getString(R.string.search));

    Drawable searchIcon = getResources().getDrawable(R.drawable.ic_action_view_search);
    int textSize = (int) (text.getTextSize() * 1.5);
    searchIcon.setBounds(0, 0, textSize, textSize);
    magHint.setSpan(new ImageSpan(searchIcon), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Set the new hint text
    text.setHint(magHint);

}

if (searchCloseIcon != null){
    searchCloseIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_close));
}

They don't expose the ids publicly for the non appcompat SearchView, but they do for AppCompat if you know where to look. :)

Trisyllable answered 16/12, 2014 at 5:21 Comment(1)
This actually works pretty well on various devices. Nice one.Assortment
T
5

I had this problem, and this works for me.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.customer_menu, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView       = (SearchView) menu.findItem(R.id.menu_customer_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        searchView.setOnQueryTextListener(this);

        //Applies white color on searchview text
        int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) searchView.findViewById(id);
        textView.setTextColor(Color.WHITE);

        return true;
}
Tractable answered 1/3, 2013 at 14:41 Comment(1)
I allways get null on: (TextView) searchView.findViewById(id);Instrumentalist
S
4

If you base your app theme on the holo theme you will get a white instead of a black text in your SearchView

<style name="Theme.MyTheme" parent="android:Theme.Holo">

I did not find any other way to change the textcolor of the searchview without using dirty hacks.

Scalf answered 17/10, 2012 at 9:21 Comment(1)
This didn't work. I was hoping it would, since it's a lot cleaner than manually searching for a "magic" view ID. I tried several themes, with an activity whose layout has a dark background, but none of them produced anything other than black text in the SearchView.Genethlialogy
B
4

Yes it is possible using following method.

public static EditText setHintEditText(EditText argEditText, String argHintMessage, boolean argIsRequire) {
    try {
        if (argIsRequire) {
            argHintMessage = "   " + argHintMessage;
            //String text = "<font color=#8c8c8c>"+argHintMessage+"</font> <font color=#cc0029>*</font>";
            String text = "<font color=#8c8c8c>" + argHintMessage + "</font>";
            argEditText.setHint(Html.fromHtml(text));
        } else {
            argEditText.setHint(argHintMessage);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return argEditText;
}

Calling of this method look like this..

metLoginUserName=(EditText)this.findViewById(R.id.etLoginUserName);
    metLoginPassword=(EditText)this.findViewById(R.id.etLoginPassword);

    /**Set the hint in username and password edittext*/
    metLoginUserName=HotSpotStaticMethod.setHintEditText(metLoginUserName, getString(R.string.hint_username),true);
    metLoginPassword=HotSpotStaticMethod.setHintEditText(metLoginPassword, getString(R.string.hint_password),true);

using it i have successfully add red color * mark in hint using this method. You should change this method according to your requirement. I hope its helpful to you ....:)

Badinage answered 31/12, 2012 at 8:23 Comment(6)
it's somewhat a little complicated to me.Jerejereld
String text = "<font color=#8c8c8c>"+HintMessage+"</font>"; argEditText.setHint(Html.fromHtml(text)); your can also use only these two line code for setting color hintBadinage
SearchAutoComplete autoCompleteView = (SearchAutoComplete) mSearchView .findViewById(com.android.internal.R.id.search_src_text);autoCompleteView.setHintTextColor(R.color.hint_text_color); that's my solution, but i think yours is better than mine, i'll use your solution,thank you.Jerejereld
@Jerejereld have you got solution..?Badinage
@gaols,where is your "com.android.internal.R.id.search_src_text"?Ran
@Jerejereld answer is for appcompat-v7. See abc_search_view.xml, within is a view with class android.support.v7.widget.SearchView$SearchAutoCompleteIronic
P
4

Most cleanest way is:

Toolbar uses theme ThemeOverlay.AppCompat.Dark.Actionbar.

Now make child of it like:

toolbarStyle parent "ThemeOverlay.AppCompat.Dark.Actionbar"

add this item in that style

item name "android:editTextColor">yourcolor

Done.

One more very important thing is, in toolbar put the layout_height ="?attr/actionbarSize". By default it is wrap_content.For me text was not even visible in searchview it fixed that problem.

Philina answered 7/8, 2017 at 11:37 Comment(0)
S
4

This works for me, if you use androidx.appcompat.widget.SearchView apply androidx.appcompat.R.id.search_src_text to identify the searchview, no returns null in the textView

    TextView textView = (TextView) searchView.findViewById(androidx.appcompat.R.id.search_src_text);
    textView.setTextColor(Color.RED);
    textView.setHintTextColorstrong text(Color.RED);
Sidsida answered 8/9, 2020 at 19:11 Comment(0)
B
3

Use this one, it's right. :D

AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchText.setHintTextColor(getResources().getColor(color.black));
searchText.setTextColor(getResources().getColor(color.black));
Blighter answered 28/1, 2013 at 10:27 Comment(0)
H
3

Change color of typed text:

((EditText)((SearchView)findViewById(R.id.searchView)).findViewById(((SearchView)findViewById(R.id.searchView)).getContext().getResources().getIdentifier("android:id/search_src_text", null, null))).setTextColor(Color.WHITE);

Change color of hint text:

((EditText)((SearchView)findViewById(R.id.searchView)).findViewById(((SearchView)findViewById(R.id.searchView)).getContext().getResources().getIdentifier("android:id/search_src_text", null, null))).setHintTextColor(Color.LTGRAY);
Heterotrophic answered 17/7, 2013 at 22:18 Comment(0)
T
3

Yes we can,

SearchView searchView = (SearchView) findViewById(R.id.sv_symbol);

To apply the white color for the SerachView Text,

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

Happy coding !!!!

Testudinal answered 10/4, 2015 at 5:38 Comment(0)
P
3

this is working for me.

final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);

searchView.setOnQueryTextListener(this);   
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));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) 
{
    searchEditText.setBackgroundColor(getResources().getColor(R.color.c_trasnparent));
    searchEditText.setGravity(Gravity.CENTER);
    searchEditText.setCompoundDrawables(null,null,R.drawable.ic_cross,null);
}
Paralyse answered 26/8, 2016 at 7:31 Comment(0)
D
2
TextView textView = (TextView) searchView.findViewById(R.id.search_src_text);
textView.setTextColor(Color.BLACK);
Dame answered 7/3, 2014 at 22:51 Comment(0)
F
1

A SearchView object extends from LinearLayout, so it holds other views. The trick is to find the view holding the hint text and change the colour programmatically. The problem with trying to find the view by id is that the id is dependent from the theme used in the application. So depending on the theme used, the findViewById(int id) method might return null. A better approach that works with every theme is to traverse the view hierarchy and find the widget containing the hint text:

// get your SearchView with its id
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
// traverse the view to the widget containing the hint text
LinearLayout ll = (LinearLayout)searchView.getChildAt(0);
LinearLayout ll2 = (LinearLayout)ll.getChildAt(2);
LinearLayout ll3 = (LinearLayout)ll2.getChildAt(1);
SearchView.SearchAutoComplete autoComplete = (SearchView.SearchAutoComplete)ll3.getChildAt(0);
// set the hint text color
autoComplete.setHintTextColor(getResources().getColor(Color.WHITE));
// set the text color
autoComplete.setTextColor(Color.BLUE);

Using this method, you can also change the look of other widgets in the SearchView hierarchy, such as the EditText holding the search query. Unless Google decides to change the view hierarchy of a SearchView shortly, you should be able to change the appearance of the widget with this method for some time.

Fraga answered 11/3, 2014 at 4:13 Comment(0)
T
1

It is possible to customize searchview by using appcompat v7 library.I used appcompat v7 library and defined custom style for it. In drawable folder put bottom_border.xml file which looks like this:

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 <item>
  <shape >
      <solid android:color="@color/blue_color" />
  </shape>
 </item>
 <item android:bottom="0.8dp"
   android:left="0.8dp"
   android:right="0.8dp">
  <shape >
      <solid android:color="@color/background_color" />
  </shape>
 </item>

 <!-- draw another block to cut-off the left and right bars -->
 <item android:bottom="2.0dp">
  <shape >
      <solid android:color="@color/main_accent" />
  </shape>
  </item>
 </layer-list>

In values folder styles_myactionbartheme.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
  <style name="AppnewTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowBackground">@color/background</item>
    <item name="android:actionBarStyle">@style/ActionBar</item>
    <item name="android:actionBarWidgetTheme">@style/ActionBarWidget</item>
  </style> 
  <!-- Actionbar Theme -->
  <style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/main_accent</item>
    <!-- <item name="android:icon">@drawable/abc_ic_ab_back_holo_light</item> -->
  </style> 
  <style name="ActionBarWidget" parent="Theme.AppCompat.Light">
    <!-- SearchView customization-->
     <!-- Changing the small search icon when the view is expanded -->
    <!-- <item name="searchViewSearchIcon">@drawable/ic_action_search</item> -->
     <!-- Changing the cross icon to erase typed text -->
   <!--   <item name="searchViewCloseIcon">@drawable/ic_action_remove</item> -->
     <!-- Styling the background of the text field, i.e. blue bracket -->
    <item name="searchViewTextField">@drawable/bottom_border</item>
     <!-- Styling the text view that displays the typed text query -->
    <item name="searchViewAutoCompleteTextView">@style/AutoCompleteTextView</item>        
  </style>

    <style name="AutoCompleteTextView" parent="Widget.AppCompat.Light.AutoCompleteTextView">
     <item name="android:textColor">@color/text_color</item>
   <!--   <item name="android:textCursorDrawable">@null</item> -->
    <!-- <item name="android:textColorHighlight">@color/search_view_selected_text</item> -->
  </style>
 </resources>

I defined custommenu.xml file for displaying menu:

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:com.example.actionbartheme="http://schemas.android.com/apk/res-auto" >  

  <item android:id="@+id/search"
      android:title="@string/search_title"
      android:icon="@drawable/search_buttonn"
      com.example.actionbartheme:showAsAction="ifRoom|collapseActionView"
        com.example.actionbartheme:actionViewClass="android.support.v7.widget.SearchView"/>        
  </menu>

Your activity should extend ActionBarActivity instead of Activity. Here is onCreateOptionsMenu method.

  @Override
  public boolean onCreateOptionsMenu(Menu menu) 
  {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.custommenu, menu);
  }

In manifest file:

  <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppnewTheme" >

For more information see this url:
Here http://www.jayway.com/2014/06/02/android-theming-the-actionbar/

Telesis answered 22/9, 2014 at 10:57 Comment(0)
I
1

for appcompat-v7 Toolbar with searchView (provided via MenuItemCompat):

Setting the Toolbar theme to @style/ThemeOverlay.AppCompat.Light will yield dark color (black) for the hint text and for the entered text, but won't affect the cursor* color. Accordingly, setting the Toolbar theme to @style/ThemeOverlay.AppCompat.Dark will yield light color (white) for the hint text and the entered text, the cursor* will be white anyway.

Customising the above themes:

android:textColorPrimary --> color of the entered text

editTextColor --> color of the entered text (will override the affect of android:textColorPrimary if set)

android:textColorHint --> color of the hint

*note: Am yet to determine how the Cursor color can be controlled (without using the reflection solution).

Ironic answered 23/1, 2015 at 1:31 Comment(1)
The cursor color comes from colorControlActivatedSyntactics
J
1

By using this I was able to change the typed color text in search view

AutoCompleteTextView typed_text = (AutoCompleteTextView) inputSearch.findViewById(inputSearch.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
typed_text.setTextColor(Color.WHITE);
Jazmin answered 22/5, 2016 at 15:31 Comment(0)
T
1

Wow. A lot of answer. It gets color value from your primary color.

Change it, and its done!

@Override
public void onResume() {
    super.onResume();
    getActivity().setTheme(R.style.YourTheme_Searching);
}

Styles;

<style name="YourTheme.Searching" parent="YourTheme">
    <item name="android:textColorPrimary">@android:color/white</item>
</style>
Tootsy answered 12/4, 2017 at 18:19 Comment(0)
J
1

change searchView style in toolbar with androidx.

firstly,set search view style in your toolbar Style (change parent them with your own app base them):

     <!-- toolbar style -->
      <style name="ToolbarStyle" parent="Theme.AppCompat.Light.NoActionBar">     
          <!-- search view about -->
          <item name="android:editTextColor">@color/colorWhitePrimaryText</item>
          <item name="android:textColorHint">@color/colorWhiteSecondaryText</item>
          <item name="android:textCursorDrawable">@drawable/drawable_cursor_white</item>
          <item name="closeIcon">@mipmap/ic_close</item>
          <item name="searchHintIcon">@mipmap/ic_search_white</item>
          <item name="searchIcon">@mipmap/ic_search_white</item>
          <item name="android:longClickable">false</item>
          <item name="android:queryHint">@string/toolbar_search</item>
      </style> 

then, use it in your toolbar layout:

android:theme="@style/ToolbarStyle"

with this, you can change most style of searchView except for query hint.

finally set query hint at your toolbar menu's options:

toolbar.setOnMenuItemClickListener(item -> {
        switch (item.getItemId()){
            case R.id.toolbar_search:
                SearchView searchView = (SearchView) item.getActionView();
                searchView.setQueryHint("default query");
                searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        return false;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        return false;
                    }
                });
                return true;
                default:
                    return false;
        }
Judaism answered 11/11, 2019 at 17:10 Comment(2)
Did you mean to post something under "searchView style"?Oversweet
I have completed and simplified my solution,maybe it can help you.Judaism
P
1

Kotlin way of changing text/hint color using extension

fun SearchView.changeColors(color: Int) {
    val editText = this.findViewById<EditText>(androidx.appcompat.R.id.search_src_text)
    editText.setTextColor(ContextCompat.getColor(context, color))
    editText.setHintTextColor(ContextCompat.getColor(context, color))
}
Plinth answered 13/6, 2022 at 18:2 Comment(0)
F
1

With Kotlin ad AndroidX, just try the following:

val id: Int = binding.searchView.context
        .resources
        .getIdentifier("android:id/search_src_text", null, null)
val editText: EditText = binding.searchView.findViewById(id)
editText.setTextColor(Color.BLUE)
editText.setHintTextColor(Color.BLACK)
Fujio answered 13/7, 2022 at 1:41 Comment(1)
using getIdentifier is not recommended anymoreMaple
L
0
searchView = (SearchView) view.findViewById(R.id.searchView);

SearchView.SearchAutoComplete searchText = (SearchView.SearchAutoComplete) searchView
      .findViewById(org.holoeverywhere.R.id.search_src_text);
searchText.setTextColor(Color.BLACK);

I am using Holoeverywhere Library. Note the org.holoeverywhere.R.id.search_src_text

Listing answered 1/3, 2014 at 7:44 Comment(0)
B
0

I found a solution from a blog post. See here.

Basically you style searchViewAutoCompleteTextView and have android:actionBarWidgetTheme inherit the style.

Bernadinebernadotte answered 5/10, 2014 at 15:38 Comment(0)
U
0

it works with me

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem searchItem = menu.findItem(R.id.action_search);
    EditText searchEditText = (EditText) searchView.getActionView().findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchEditText.setTextColor(getResources().getColor(R.color.white));
    searchEditText.setHintTextColor(getResources().getColor(R.color.white));
    return super.onPrepareOptionsMenu(menu);
}
Ung answered 19/4, 2016 at 13:47 Comment(0)
B
0

What worked for me

((EditText)searchView.findViewById(R.id.search_src_text)).setTextColor(getResources().getColor(R.color.text));
Biarritz answered 2/8, 2018 at 7:36 Comment(0)
F
0

For Kotlin Language

    searchView = view.findViewById(R.id.searchView_Contacts)
    // change color
    val id = searchView.context.resources
        .getIdentifier("android:id/search_src_text", null, null)

    val textView = searchView.findViewById<View>(id) as TextView

    textView.setTextColor(Color.WHITE)
Fireball answered 5/5, 2020 at 12:34 Comment(0)
R
0

This works for me. *Kotlin

   val searchEditText = searchView.findViewById<View>(androidx.appcompat.R.id.search_src_text) as EditText
        searchEditText.setTextColor(getColor(R.color.appBarItemColor))

Use androidx.appcompat.R.id.search_src_text if you've migrated your project to AndroidX. if not use android.support.v7.appcompat.R.id.search_src_text

Ruck answered 12/4 at 5:6 Comment(0)
B
-1

You can implement like this class to change font color and image ::

import com.actionbarsherlock.widget.SearchView;
import com.actionbarsherlock.widget.SearchView.SearchAutoComplete;


public class MySearchView {
    public static SearchView getSearchView(Context context, String strHint) {
        SearchView searchView = new SearchView(context);
        searchView.setQueryHint(strHint);
        searchView.setFocusable(true);
        searchView.setFocusableInTouchMode(true);
        searchView.requestFocus();
        searchView.requestFocusFromTouch();

         ImageView searchBtn = (ImageView) searchView.findViewById(R.id.abs__search_button);
         searchBtn.setImageResource(R.drawable.ic_menu_search);


         ImageView searchBtnClose = (ImageView) searchView.findViewById(R.id.abs__search_close_btn);
         searchBtnClose.setImageResource(R.drawable.ic_cancel_search_bar);


         SearchAutoComplete searchText = (SearchAutoComplete) searchView.findViewById(R.id.abs__search_src_text);

        searchText.setTextColor(context.getResources().getColor(color.white));

        return searchView;
    }


    public static SearchView getSearchView(Context context, int strHintRes) {
        return getSearchView(context, context.getString(strHintRes));
    }
}

I hope it can help you guys. :D

Blighter answered 3/10, 2013 at 17:43 Comment(0)
B
-2

I tried and find one solution for this. I think it will help you..

searchView.setBackgroundColor(Color.WHITE);

enter image description here

Basis answered 15/8, 2012 at 7:38 Comment(1)
the question says text color not background colorKendry
C
-2

This Way :)

View searchPlate = searchView.findViewById(searchPlateId);
if (searchPlate!=null) {
    searchPlate.setBackgroundColor(Color.DKGRAY);
    int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
    if (searchText != null) {
        searchText.setTextColor(Color.WHITE);
        searchText.setHintTextColor(Color.WHITE);
    }
}
return ;
}
Callery answered 10/3, 2018 at 7:23 Comment(2)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Mendoza
thanx for help .Callery
S
-3

Casting the SearchView to TextView allows you to use TextView's methods to change its colour:

((TextView) searchView).setTextColor(Color.WHITE);
((TextView) searchView).setHintTextColor(Color.WHITE);
Supersensitive answered 7/7, 2015 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.