Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set SearchView clear button color

I am creating a searchview in the toolbar using onCreateOptionsMenu, but can't get the clear X button to initially be white. It becomes white when starting to type letters. It also stays white after clearing.

Example of clear button having the color of gray instead of white

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.responsible_menu, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); //TODO: May not be needed?

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
    {
        @Override
        public boolean onQueryTextSubmit(String query)
        {
            mAdapter.updateUIWithFilter(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText)
        {
            mAdapter.updateUIWithFilter(newText);
            return false;
        }
    });

    // Does not work! Still not white.
    ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchClose.setColorFilter(Color.argb(255, 255, 255, 255));

    searchClose.setAlpha(255);

    return true;
}

responsible_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:appcompat="http://schemas.android.com/apk/res-auto"
      xmlns:app="http://schemas.android.com/tools">

    <item
        android:id="@+id/menu_search"
        android:title="@string/search"
        appcompat:actionViewClass="android.support.v7.widget.SearchView"
        appcompat:showAsAction="always"/>

</menu>
like image 391
Sunkas Avatar asked Aug 24 '15 09:08

Sunkas


People also ask

How to change the icons of a searchview in Android?

Expand the search view by setting iconified and iconifiedByDefault flags to false. Find views inside the search view and set the colors as per your wish. Find views of the icons you wish to change and replace them with your icons. And it’s done. SearchView my_search_view = (SearchView) MenuItemCompat.getActionView(searchItem);

Is there a way to change the text color of searchview?

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?

How to make the searchview always visible in action bar?

Important Note: When a SearchView is used in an Action Bar as an action view for collapsible menu item then it needs to be set to iconified by default using setIconfiedByDefault (true) function. If you want the search field to always be visible, then call setIconifiedByDefault (false). true is the default value for this function.

How to display a searchview and listview using its different attributes?

In this step we open an xml file and add the code for displaying a SearchView and ListView by using its different attributes. In this step we open MainActivity and add the code to initiate SearchView and ListView. In this we create an Animal name list and then set the adapter to fill the data in ListView.


Video Answer


3 Answers

Found a solution. However, interested in better ones.

Downloaded the "clear button" image from https://www.google.com/design/icons/#ic_clear in 24pt white and added this code to the end of onCreateOptionsMenu

    // Does help!
    ImageView searchClose = (ImageView) searchView.findViewById(androidx.appcompat.R.id.search_close_btn);
    searchClose.setImageResource(R.drawable.ic_clear_white_24dp);
like image 88
Sunkas Avatar answered Oct 12 '22 06:10

Sunkas


You can also set it from XML. I'm using the support library 24.2.1 and an activity with a theme whose parent is "Theme.AppCompat.Light" and an android.support.v7.widget.SearchView in the ActionBar.

You can set an actionBarWidgetTheme in your activity theme like this:

<style name="ActivityTheme" parent="Theme.AppCompat.Light">
    <item name="actionBarWidgetTheme">@style/MyActionBarWidgetTheme</item>
</style>

And then define MyActionBarWidgetTheme and set the colours you want there:

<style name="MyActionBarWidgetTheme">
    <item name="android:textColorSecondary">@android:color/white</item>
</style>
  • android:textColorSecondary applies to the close icon

  • android:textColorPrimary applies to the text you enter

  • colorControlActivated applies to the cursor

  • android:textColorHint applies to the hint text

like image 35
Bea Avatar answered Oct 12 '22 06:10

Bea


Try this:

    ImageView searchClose = searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchClose.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
like image 5
wbk727 Avatar answered Oct 12 '22 04:10

wbk727