Search code examples
androidandroid-actionbarsearchview

Customize close icon in searchview


How can I change the res of closeIcon of searchview. Using Holo.light as parent. I have tried lots of things! This name isn't on Holo!!

<item name="android:searchViewCloseIcon">@drawable/ic_search_close</item>

Solution

  • If you're using AppCompat v21 compile 'com.android.support:appcompat-v7:22.2.0' you can use this:

    values/themes.xml:
    <style name=”Theme.MyTheme” parent=”Theme.AppCompat”>
        <item name=”searchViewStyle”>@style/MySearchViewStyle</item>
    </style>
    <style name=”MySearchViewStyle” parent=”Widget.AppCompat.SearchView”>
        <!-- Background for the search query section (e.g. EditText) -->
        <item name="queryBackground">...</item>
        <!-- Background for the actions section (e.g. voice, submit) -->
        <item name="submitBackground">...</item>
        <!-- Close button icon -->
        <item name="closeIcon">...</item>
        <!-- Search button icon -->
        <item name="searchIcon">...</item>
        <!-- Go/commit button icon -->
        <item name="goIcon">...</item>
        <!-- Voice search button icon -->
        <item name="voiceIcon">...</item>
        <!-- Commit icon shown in the query suggestion row -->
        <item name="commitIcon">...</item>
        <!-- Layout for query suggestion rows -->
        <item name="suggestionRowLayout">...</item>
    </style>
    

    Found here: http://android-developers.blogspot.nl/2014/10/appcompat-v21-material-design-for-pre.html

    If you want consistency I would advice to use this library.

    If you really don't want to use this library you can use this to change the close icon:

    int closeButtonId = getResources().getIdentifier("android:id/search_close_btn", null, null);  
    ImageView closeButtonImage = (ImageView) searchView.findViewById(closeButtonId);  
    closeButtonImage.setImageResource(R.drawable.ic_action_cancel);  
    

    Got it from here: http://nlopez.io/how-to-style-the-actionbar-searchview-programmatically/