4

I want to change the color of a text in an ActionBarSherlock SearchView, so I found this on StackOverflow:

AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchText.setHintTextColor(getResources().getColor(R.color.white));
searchText.setTextColor(getResources().getColor(R.color.white));    

But the problem is that searchText is null always. Also I tried with search_src_text (no abs__) - again, searchText is null.

I use Sherlock for my ActionBar (having custom view for the actionbar) but I also want the text color of the search view to be changed.

Anything I don't get?

Trinimon
  • 13,839
  • 9
  • 44
  • 60
user584513
  • 630
  • 11
  • 20
  • How does your layout look like and on which version of Android is your app running? It's quite common to add and apply some custom styles instead of assigning them programmatically. `abs__...` identifiers are only (potentially) useful on pre-Honeycomb devices. – Trinimon Jun 04 '13 at 17:09
  • my actionbar's custom view layout is simple LinearLayout which contains two imageview's. The Android I run the app is 4.2.. – user584513 Jun 04 '13 at 17:15
  • http://stackoverflow.com/questions/11085308/changing-the-background-drawable-of-the-searchview-widget check this – karan Jun 04 '13 at 17:25

2 Answers2

7

Change and apply your styles as follows:

  1. Overwrite SearchViewStyle that defines relevant style.

    <style name="SearchViewStyle" 
           parent="Widget.Sherlock.Light.SearchAutoCompleteTextView">
        <item name="android:textColor">myTextColor</item>
    </style>
    
  2. Add this style definition to your app theme.

    <style name="CustomTheme" parent="Theme.Sherlock.Light">
        ...
        <item name="searchAutoCompleteTextView">@style/SearchViewStyle</item>
        ...
    </style>
    
  3. Apply this theme in your Manifest (android:theme="CustomTheme").

Hope this helps ... Cheers!

p.s. search for SearchViewStyle in ABS style definitions (XML files). This way you can follow up available styles using Ctrl+click on definitions.

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • Any advice on how to get the cursor to be white as well? – QED Aug 19 '13 at 22:43
  • 1
    Have you seen this: http://stackoverflow.com/questions/5382571/display-cursor-in-autocompletetextview-android-honeycomb-action-bar or that one: http://stackoverflow.com/questions/15527420/custom-cursor-color-in-searchview ? – Trinimon Aug 20 '13 at 06:48
0

other way, work for me

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        searchView = new SearchView(getSupportActionBar().getThemedContext());

        AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
        searchText.setTextColor(Color.BLACK); 
.....................
}
vuhung3990
  • 6,353
  • 1
  • 45
  • 44