4

I have my own theme for ActionBarSherlock based on Theme.Sherlock.Light.DarkActionBar, here are my styles:

<style name="Theme.MyTheme" parent="Theme.Sherlock.Light.DarkActionBar">
    <item name="actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
</style>

<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/action_bar</item>
    <item name="background">@drawable/action_bar</item>
</style>

Where @drawable/action_bar is an xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/color_action_bar_bottom_line" />
        </shape>
    </item>

    <item android:bottom="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/color_action_bar_background" />
        </shape>
    </item>
</layer-list>

Everything looks great except one thing: the text in the SearchView (when I use the search mode in ActionBar) is "dark on dark", so I cannot see anything. I tried to set the text colors for SearchView manually:

AutoCompleteTextView searchTextView =
        (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchTextView.setTextColor(Color.WHITE);
searchTextView.setHintTextColor(Color.LTGRAY);
searchTextView.setHighlightColor(Color.WHITE);
searchTextView.setLinkTextColor(Color.WHITE);

This helped a lot but I cannot change the color of autocomplete text:

enter image description here

As you see, it's still black. How can I set the white text color for this kind of text?

Andrew
  • 3,696
  • 3
  • 40
  • 71

6 Answers6

3

Try this code

SearchView searchView = new SearchView(context);
LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);
autoComplete.setTextColor(Color.WHITE);

or

SearchView searchView = new SearchView(context);
AutoCompleteTextView search_text = (AutoCompleteTextView) searchView.findViewById(searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
search_text.setTextColor(Color.WHITE);

It's from my own answer here How to set SearchView TextSize?

Community
  • 1
  • 1
Pongpat
  • 13,248
  • 9
  • 38
  • 51
  • The first solution works the same as my own: the text color is white, but the autocomplete "text links" are black. The second one is falling with null reference exception. – Andrew Feb 26 '13 at 07:34
2

I solved this by disabling autocomplete feature (it will be disabled if you'll set the edit type to Visible Password), but this solution is not very good and I'm still in search for better one.

private SearchView getSearchView() {
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
    searchView.setQueryHint("Search for items");
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    AutoCompleteTextView searchTextView =
            (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
    if (searchTextView != null) {
        searchTextView.setInputType(InputType.TYPE_CLASS_TEXT
                                    | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        searchTextView.setTypeface(Typeface.DEFAULT);
    }
    return searchView;
}
Andrew
  • 3,696
  • 3
  • 40
  • 71
1

I think your are using a Light Theme with a custom dark action bar theme. You could try to change the theme of the AutoCompleteTextView to a dark one. Try to add this in your theme :

<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
    <item name="searchAutoCompleteTextView">@style/Widget.Sherlock.SearchAutoCompleteTextView</item>
    <item name="android:searchAutoCompleteTextView">@style/Widget.Sherlock.SearchAutoCompleteTextView</item>

    <item name="searchAutoCompleteTextViewStyle">@style/Widget.Sherlock.SearchAutoCompleteTextView</item>
    <item name="android:searchAutoCompleteTextViewStyle">@style/Widget.Sherlock.SearchAutoCompleteTextView</item>

    <item name="textAppearanceLargePopupMenu">@style/Widget.Sherlock.SearchAutoCompleteTextView</item>
    <item name="android:textAppearanceLargePopupMenu">@style/Widget.Sherlock.SearchAutoCompleteTextView</item>

    <item name="textAppearanceSmallPopupMenu">@style/Widget.Sherlock.SearchAutoCompleteTextView</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/Widget.Sherlock.SearchAutoCompleteTextView</item>
</style>

Edit : not sure about the added lines but you can try. I spot it in some fixes for the HoloEverywhere library.

Rémi F
  • 1,327
  • 12
  • 25
  • This didn't help. The autocomplete text is still black. I looked at the ActionBarSherlock examples, their standard example `SearchView` has the same problem. – Andrew Feb 22 '13 at 09:48
  • I edited my answer using info from https://github.com/ChristopheVersieux/HoloEverywhere/commit/b839f5df0cef9a093aaac0992abcb8e8d33cf011 – Rémi F Feb 26 '13 at 19:28
  • Android cannot find attributes `searchAutoCompleteTextViewStyle` and `android:searchAutoCompleteTextViewStyle`. Where can I find them? I have ActionBarSherlock 4.2.0, targetSDK: `17`, minSDK: `8` If I comment them in code, I have the same situation, the text color of editor is white, but autocomplete suggestions are black. – Andrew Feb 27 '13 at 09:12
1

May be there are is best way to do this , but used the very simple way, I used library for that

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        searchMenuItem = CollapsibleMenuUtils.addSearchMenuItem(menu, false,
                textWatcher);
        return true;
    }

set the boolean value for true/false for setting Light/Dark theme

addSearchMenuItem(Menu menu, boolean isLightTheme, TextWatcher textWatcher)

Adding collapsible search menu item to the menu.

Parameters: menu isLightTheme - true if use light them for ActionBar, else false textWatcher

for changing the text color i changed the librery's layout/ holodark.xml

<AutoCompleteTextView
        android:id="@+id/search_src_text"
        android:layout_width="0dp"
       .
       .
        android:textColor="@android:color/white" />
LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
1

Here iam useing SearchView like this it's working nice searchtext color also in white color.

this one res/menu and name is applications_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/Accounts"
    android:icon="@drawable/ic_launcher">
</item>
<item
    android:id="@+id/menu_search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@drawable/ic_launcher"
    android:showAsAction="ifRoom"
    android:title="menu_search">
</item>
</menu>

this one use in activity when you search like this

new MenuInflater(this).inflate(R.menu.applications_menu, menu);     
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));           
    searchView.setIconifiedByDefault(false);

here is screen

enter image description here

NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
1

The issue is that the SearchView is initialized without properly considering the ActionBar style (in this case Dark ActionBar on Light) that is set. And the answer is to use the ActionBar themed Context; retrieved by Activity.getActionBar().getThemedContext(). This is mentioned at the bottom of ABS Migration:

If you are using list navigation pay special attention to the 'List Navigation' example in the demos sample for direction on proper use. You need to use the library-provided layouts for both the item view and dropdown view as well as use the themed context from the action bar.

My code is how it is done in standard Android, you'd have to get the ActionBar however appropriate. If you pass the themed Context to the SearchView then it will use the appropriate style, namely, it won't render itself as if it were on a light background.

Dandre Allison
  • 5,975
  • 5
  • 42
  • 56