126

I'm trying to change the drawable that sits in the Android actionbar searchview widget.

Currently it looks like this: enter image description here

but I need to change the blue background drawable to a red colour.

I've tried many things short of rolling my own search widget, but nothing seems to work.

Can somebody point me in the right direction to changing this?

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
Martyn
  • 16,432
  • 24
  • 71
  • 104
  • 1
    I believe [this SO post](http://stackoverflow.com/questions/9019512/how-can-i-change-the-touch-effect-color-of-the-actionbar-in-android-3-0-and-high) can help you with what you want to accomplish, if you have not checked it already. – AggelosK Jul 25 '12 at 17:15
  • @Angelo Unfortunately, this is not possible with `SearchView` background because it's not available in through `R.styleable`. My answer below describes how to do it in code. – vArDo Aug 13 '12 at 21:18
  • How did you get the text "Enter film name" ? – Zen Dec 15 '14 at 00:08
  • You can set the hint text in the search view like this: SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); searchAutoComplete.setHint("hint text"); – Dominik May 04 '15 at 14:34
  • I explained in the end of this post. with images [https://stackoverflow.com/questions/47426541/how-to-change-searchbar-cancel-button-image-in-xamarin-forms/51297028#51297028](https://stackoverflow.com/questions/47426541/how-to-change-searchbar-cancel-button-image-in-xamarin-forms/51297028#51297028) – vu ledang Jul 12 '18 at 03:56

12 Answers12

259

Intro

Unfortunately there's no way to set SearchView text field style using themes, styles and inheritance in XML as you can do with background of items in ActionBar dropdown. This is because selectableItemBackground is listed as styleable in R.stylable, whereas searchViewTextField (theme attribute that we're interested in) is not. Thus, we cannot access it easily from within XML resources (you'll get a No resource found that matches the given name: attr 'android:searchViewTextField' error).

Setting SearchView text field background from code

So, the only way to properly substitute background of SearchView text field is to get into it's internals, acquire access to view that has background set based on searchViewTextField and set our own.

NOTE: Solution below depends only on id (android:id/search_plate) of element within SearchView, so it's more SDK-version independent than children traversal (e.g. using searchView.getChildAt(0) to get to the right view within SearchView), but it's not bullet-proof. Especially if some manufacturer decides to reimplement internals of SearchView and element with above-mentioned id is not present - the code won't work.

In SDK, the background for text field in SearchView is declared through nine-patches, so we'll do it the same way. You can find original png images in drawable-mdpi directory of Android git repository. We're interested in two image. One for state when text field is selected (named textfield_search_selected_holo_light.9.png) and one for where it's not (named textfield_search_default_holo_light.9.png).

Unfortunately, you'll have to create local copies of both images, even if you want to customize only focused state. This is because textfield_search_default_holo_light is not present in R.drawable. Thus it's not easily accessible through @android:drawable/textfield_search_default_holo_light, which could be used in selector shown below, instead of referencing local drawable.

NOTE: I was using Holo Light theme as base, but you can do the same with Holo Dark. It seems that there's no real difference in selected state 9-patches between Light and Dark themes. However, there's a difference in 9-patches for default state (see Light vs Dark). So, probably there's no need to make local copies of 9-patches for selected state, for both Dark and Light themes (assuming that you want to handle both, and make them both look the same as in Holo Theme). Simply make one local copy and use it in selector drawable for both themes.

Now, you'll need to edit downloaded nine-patches to your need (i.e. changing blue color to red one). You can take a look at file using draw 9-patch tool to check if it is correctly defined after your edit.

I've edited files using GIMP with one-pixel pencil tool (pretty easy) but you'll probably use the tool of your own. Here's my customized 9-patch for focused state:

enter image description here

NOTE: For simplicity, I've used only images for mdpi density. You'll have to create 9-patches for multiple screen densities if, you want the best result on any device. Images for Holo SearchView can be found in mdpi, hdpi and xhdpi drawable.

Now, we'll need to create drawable selector, so that proper image is displayed based on view state. Create file res/drawable/texfield_searchview_holo_light.xml with following content:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"
        android:drawable="@drawable/textfield_search_selected_holo_light" />
    <item android:drawable="@drawable/textfield_search_default_holo_light" />
</selector>

We'll use the above created drawable to set background for LinearLayout view that holds text field within SearchView - its id is android:id/search_plate. So here's how to do this quickly in code, when creating options menu:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }       

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);

        // Getting SearchView from XML layout by id defined there - my_search_view in this case
        SearchView searchView = (SearchView) menu.findItem(R.id.my_search_view).getActionView();
        // Getting id for 'search_plate' - the id is part of generate R file,
        // so we have to get id on runtime.
        int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
        // Getting the 'search_plate' LinearLayout.
        View searchPlate = searchView.findViewById(searchPlateId);
        // Setting background of 'search_plate' to earlier defined drawable.            
        searchPlate.setBackgroundResource(R.drawable.textfield_searchview_holo_light);          

        return super.onCreateOptionsMenu(menu);
    }

}

Final effect

Here's the screenshot of the final result:

enter image description here

How I got to this

I think it's worth metioning how I got to this, so that this approach can be used when customizing other views.

Checking out view layout

I've checked how SearchView layout looks like. In SearchView contructor one can find a line that inflates layout:

inflater.inflate(R.layout.search_view, this, true);

Now we know that SearchView layout is in file named res/layout/search_view.xml. Looking into search_view.xml we can find an inner LinearLayout element (with id search_plate) that has android.widget.SearchView$SearchAutoComplete inside it (looks like ours search view text field):

    <LinearLayout
        android:id="@+id/search_plate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:orientation="horizontal"
        android:background="?android:attr/searchViewTextField">

Now, we now that the background is set based on current theme's searchViewTextField attribute.

Investigating attribute (is it easily settable?)

To check how searchViewTextField attribute is set, we investigate res/values/themes.xml. There's a group of attributes related to SearchView in default Theme:

<style name="Theme">
    <!-- (...other attributes present here...) -->

    <!-- SearchView attributes -->
    <item name="searchDropdownBackground">@android:drawable/spinner_dropdown_background</item>
    <item name="searchViewTextField">@drawable/textfield_searchview_holo_dark</item>
    <item name="searchViewTextFieldRight">@drawable/textfield_searchview_right_holo_dark</item>
    <item name="searchViewCloseIcon">@android:drawable/ic_clear</item>
    <item name="searchViewSearchIcon">@android:drawable/ic_search</item>
    <item name="searchViewGoIcon">@android:drawable/ic_go</item>
    <item name="searchViewVoiceIcon">@android:drawable/ic_voice_search</item>
    <item name="searchViewEditQuery">@android:drawable/ic_commit_search_api_holo_dark</item>
    <item name="searchViewEditQueryBackground">?attr/selectableItemBackground</item>

We see that for default theme the value is @drawable/textfield_searchview_holo_dark. For Theme.Light value is also set in that file.

Now, it would be great if this attribute was accessible through R.styleable, but, unfortunately it's not. For comparison, see other theme attributes which are present both in themes.xml and R.attr like textAppearance or selectableItemBackground. If searchViewTextField was present in R.attr (and R.stylable) we could simply use our drawable selector when defining theme for our whole application in XML. For example:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppTheme" parent="android:Theme.Light">
        <item name="android:searchViewTextField">@drawable/textfield_searchview_holo_light</item>
    </style>
</resources>

What should be modified?

Now we know, that we'll have to access search_plate through code. However, we still don't know how it should look like. In short, we search for drawables used as values in default themes: textfield_searchview_holo_dark.xml and textfield_searchview_holo_light.xml. Looking at content we see that the drawable is selector which reference two other drawables (which occur to be 9-patches later on) based on view state. You can find aggregated 9-patch drawables from (almost) all version of Android on androiddrawables.com

Customizing

We recognize the blue line in one of the 9-patches, so we create local copy of it and change colors as desired.

Community
  • 1
  • 1
vArDo
  • 4,408
  • 1
  • 17
  • 26
  • Your java code will not work, his SearchView is in the ActionBar and can't be found with the findViewById method. But the rest is correct! – VinceFR Jul 26 '12 at 14:16
  • @VinceFR Thanks for pointing that out. Haven't noticed it (I've focused on `SearchView` itself). Is it OK if I merge your answer (overriding onCreateOptionsMenu) into mine? – vArDo Jul 26 '12 at 14:23
  • @VinceFR I've moved my code from `onCreate()` method to `onCreateOptionsMenu()` method as you suggested. Voted your answer up. – vArDo Jul 26 '12 at 14:43
  • Thanks a lot :D is there any way i can change the Mag icon and the Close Icon in a similar way ? – Harsha M V Dec 21 '12 at 18:26
  • How can we change a magnifier icon? I try everything, but i can't change it... Thanks... and how to do this in sherlock action bar? – Jovan Mar 21 '13 at 16:12
  • @Jovan Have you created separate question for your problem? – vArDo Mar 22 '13 at 09:05
  • No, but I figured it out...it was in xml :) Thanks anyway... But is there any way to set some image over action bar? As a tutorial (press this for sending to friends or something similar)... If you know how I will open a question... I think that this can help to others to... :) – Jovan Mar 22 '13 at 11:16
  • @Jovan Why don't you just create additional overlay view with that information and show it? You pretty much always know where action bar is on the screen. – vArDo Mar 22 '13 at 11:46
  • @Jovan BTW: asking questions as separate postings is always encouraged on SO. They have more visibility than discussions in comment :) – vArDo Mar 22 '13 at 12:11
  • I opened a new question regarding this. Here is a link: http://stackoverflow.com/questions/15618537/how-to-put-overlay-view-over-action-bar-sherlock But how to set an overlay when action bar uses the top space on layout and a fragment is displayed on the rest?! – Jovan Mar 25 '13 at 15:24
  • @Jovan Checkout my answer in that question. – vArDo Mar 26 '13 at 17:18
  • Hi, great answer! I've a bit of trouble with that solution in landscape for a condensed ActionBar. It would be great, if you could give me some input on this: http://stackoverflow.com/questions/17210496/how-to-make-custom-searchview-widget-background-work-for-landscape-actionbar. Thx! – Chris Jun 20 '13 at 12:44
  • Hey @vArDo, how come when you set the background drawable `searchPlate.setBackgroundResource(R.drawable.textfield_searchview_holo_light);` then the Holo underline appears further from the bottom of the `ActionBar` then the default styling? – Etienne Lawlor Jun 24 '13 at 08:38
  • @toobsco42 Can you provide some screenshots comparing both? If 9-patches have the same dimensions there should not be any difference. – vArDo Jun 25 '13 at 09:47
  • I just opened the 9 patch in Gimp and realized that there is additional space underneath the underline. I cut out this area of the 9 patch and now the underline appears lower in the `ActionBar`. The 9 patch was generated with this tool `http://android-holo-colors.com/`. – Etienne Lawlor Jun 25 '13 at 20:48
  • @Chris I'll try to look at your questions as soon as I can. Looks interesting. – vArDo Jun 26 '13 at 20:38
  • Great answer and very well explained, +1 for that. For anyone running into the same small problem I was left over with after applying your solution, here's the hint to solve it: When using voice-search the right part of the EditText is still colored in standard Holo-blue. The ID of the LinearLayout is "submit_area" and the 9-patch-images used for that are called "textfield_search_right_selected_holo_light.9.png" and so on. So with these information one can use your solution for that as well. – chuky Nov 28 '13 at 18:44
  • nice explanation buddy :) it solved my problem of hiding the searchview with colapse menu items – Maveňツ Feb 15 '14 at 05:06
  • @Jovan how did you change the mag-icon in the EditText? Could you please add an answer here explaining that as well? – Zen Dec 15 '14 at 00:06
33

The above solutions may not work if you are using appcompat library. You may have to modify the code to make it work for appcompat library.

Here is the working solution for appcompat library.

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {

    getMenuInflater().inflate(R.menu.main_menu, menu); 

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchAutoComplete.setHintTextColor(Color.WHITE);
    searchAutoComplete.setTextColor(Color.WHITE);

    View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    searchplate.setBackgroundResource(R.drawable.texfield_searchview_holo_light);

    ImageView searchCloseIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchCloseIcon.setImageResource(R.drawable.abc_ic_clear_normal);

    ImageView voiceIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn);
    voiceIcon.setImageResource(R.drawable.abc_ic_voice_search);

    ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
    searchIcon.setImageResource(R.drawable.abc_ic_search);


    return super.onCreateOptionsMenu(menu);
}
Abdul Rahman
  • 1,833
  • 1
  • 21
  • 21
  • 1
    this doesn't work for search_mag_icon. It gives "Style contains key with bad entry: 0x01010479" – Lavanya Dec 17 '14 at 09:34
  • 1
    the search plate doesn't work for me (it does something but I can't see any change to the bottom line), but the general idea for AppCompat is very helpful – guy_m Aug 12 '15 at 14:59
14

Your onCreateOptionsMenu method must be:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.option, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        int linlayId = getResources().getIdentifier("android:id/search_plate", null, null);
        ViewGroup v = (ViewGroup) searchView.findViewById(linlayId);
        v.setBackgroundResource(R.drawable.searchviewredversion);
        return super.onCreateOptionsMenu(menu);
    }

where your search item is menu_search off course

and here is the searchviewredversion (this one is the xhdpi version): http://img836.imageshack.us/img836/5964/searchviewredversion.png

enter image description here

VinceFR
  • 2,551
  • 1
  • 21
  • 27
  • is there any way to change the Search icon and the Close button in a similar way ? – Harsha M V Dec 21 '12 at 18:28
  • I don't know about the SherlockActionBar 4.2 But yes you can change the search icon and the close button in the same way! – VinceFR Dec 23 '12 at 10:52
  • ActionbarSherlock has it's own themes and defines its own SearchView. This means you can just add an item to your theme named searchViewTextField and using the drawable definition and 9 patches above. – NKijak Jan 09 '13 at 04:30
  • 1
    Don't forget to rename file to searchviewredversion.9.png to indicate to export asset as 9-patch – 1owk3y Nov 30 '14 at 22:16
13

The solution above doesn't work with ActionBarSherlock 4.2 and therefore it's not backward compatible to Android 2.x. Here is working code which setups SearchView background and hint text on ActionBarSherlock 4.2:

public static void styleSearchView(SearchView searchView, Context context) {
    View searchPlate = searchView.findViewById(R.id.abs__search_plate);
    searchPlate.setBackgroundResource(R.drawable.your_custom_drawable);
    AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
    searchText.setHintTextColor(context.getResources().getColor(R.color.your_custom_color));
}
David Vávra
  • 18,446
  • 7
  • 48
  • 56
  • where should i put this code ? i am trying to do this in 4.2 version of the action bar. my question http://stackoverflow.com/questions/13992424/android-sherlock-actionbar-search-view-styling – Harsha M V Dec 21 '12 at 17:25
  • Hi, call it from onCreateOptionsMenu method. Find searchView and pass it to this method. – David Vávra Dec 22 '12 at 19:52
  • Thank you. Any idea how to remove that inline Mag symbol as shown here http://i.imgur.com/YvBz2.png – Harsha M V Dec 22 '12 at 19:57
  • 3
    With ABS you will be extending Theme.Sherlock or Theme.Sherlock.Light *not* the android themes. This means all you have to do is add an item named searchViewTextField to your apps theme definition. All done in XML no code required. – NKijak Jan 09 '13 at 04:28
5

I've tired to do this as well and I'm using v7. The application was crashed when I tried to grab the searchPlate via the getIdentifier() so I done it this way:

View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
Ido
  • 2,034
  • 1
  • 17
  • 16
4

Update

If you are using AndroidX then you can do it like this

    View searchPlate = svSearch. findViewById(androidx.appcompat.R.id.search_plate);
    if (searchPlate != null) {
        AutoCompleteTextView searchText = searchPlate.findViewById(androidx.appcompat.R.id.search_src_text);
        if (searchText != null){
            searchText.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.edittext_text_size));
            searchText.setMaxLines(1);
            searchText.setSingleLine(true);
            searchText.setTextColor(getResources().getColor(R.color.etTextColor));
            searchText.setHintTextColor(getResources().getColor(R.color.etHintColor));
            searchText.setBackground(null);
        }
    }
Rashid
  • 1,700
  • 1
  • 23
  • 56
3

I also faced same problem.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"/>

Your activity should extend ActionBarActivity instead of Activity.

@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 here:
Here http://www.jayway.com/2014/06/02/android-theming-the-actionbar/

Suhas Patil
  • 131
  • 7
  • Check out: http://stackoverflow.com/questions/8211929/styling-edittext-view-with-shape-drawable-to-look-similar-to-new-holographic-the for another LayerList example. – Jared Burrows Oct 14 '14 at 17:23
2

First, let's create an XML file called search_widget_background.xml, to be used as a drawable, i.e. under drawable directory.

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

    <solid android:color="@color/red" />

</shape>

This drawable will be used as the background for our search widget. I set the color to red because that's what you asked for, but you can set it to any color defined by @color tag. You can even modify it further using the attributes defined for shape tag (make rounded corners, do an oval background, etc.).

Next step is to set the background of our search widget to this one. This can be accomplished by the following:

    public boolean onCreateOptionsMenu(Menu menu) {

        SearchView searchView = (SearchView)menu.findItem(R.id.my_search_view).getActionView();
        Drawable d = getResources().getDrawable(R.drawable.search_widget_background);
        searchView.setBackground(d);
...
    }
Erol
  • 6,478
  • 5
  • 41
  • 55
  • @shkschneider IMHO it won't work, because background has to be set on an element inside `SearchView` (named `search_plate`), not on `SearchView` itself. See my answer for more details. However, I have to admit, I haven't tried to use @(baba tenor)'s answer. – vArDo Jul 26 '12 at 13:18
  • Didn't notice the SearchView was in ActionBar. Now it should work. – Erol Jul 26 '12 at 15:54
  • @babatenor This won't work because background for `SearchView` itself will always be below background for elements that are inside `SearchView`. There's a `LinearLayout` view inside `SearchView` that has id `search_plate`, that sets has this blue underline element as background. See my answer for details. So with your solution the result is blue background for the whole widget with blue underline still visible. I've tested on *Jelly Bean*, but I don't think SDK target version matters here. – vArDo Jul 26 '12 at 18:14
  • Thank you vArDo. I knew the layered structure of SearchView but I just assumed this would work (it looked logical to me at least). – Erol Jul 26 '12 at 18:25
  • 1
    @babatenor Would be great if it worked, so that all of this hackery is not necessary :) – vArDo Jul 26 '12 at 22:21
1
public boolean onCreateOptionsMenu(Menu menu) {
........

        // Set the search plate color
        int linlayId = getResources().getIdentifier("android:id/search_plate", null, null);
        View view = searchView.findViewById(linlayId);
        Drawable drawColor = getResources().getDrawable(R.drawable.searchcolor);
        view.setBackground( drawColor );
........
    }

and this is the searchablecolor.xml file

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item>
      <shape >
          <solid android:color="#ffffff" />
      </shape>
  </item>

  <!-- main color -->
  <item android:bottom="1.5dp"
      android:left="1.5dp"
      android:right="1.5dp">
      <shape >
          <solid android:color="#2c4d8e" />
      </shape>
  </item>

  <!-- draw another block to cut-off the left and right bars -->
  <item android:bottom="18.0dp">
      <shape >
          <solid android:color="#2c4d8e" />
      </shape>
  </item>
</layer-list>
M.Raheel
  • 165
  • 7
1

I was digging about that a lot and finally I found this solution and it works for me!
You can use this

If you use appcompat try this:

ImageView searchIconView = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchIconView.setImageResource(R.drawable.yourIcon);

If you use androidx try this:

ImageView searchIconView = (ImageView) searchView.findViewById(androidx.appcompat.R.id.search_button);
    searchIconView.setImageResource(R.drawable.yourIcon);

It will change the default serchview icon.

Hope it helps!

0

I explained in the end of this post with images this is apply for xamarin forms. But i think you can understand it, because it is based on the source code of searchview of android

How to change searchbar cancel button image in xamarin forms

vu ledang
  • 465
  • 4
  • 7
0

If you are inflating Searchview like this "getMenuInflater().inflate(R.menu.search_menu, menu);". Then you can customize this searchview via style.xml like below.

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
      <item name="searchViewStyle">@style/SearchView.ActionBar</item>
</style>

<style name="SearchView.ActionBar" parent="Widget.AppCompat.SearchView.ActionBar">
      <item name="queryBackground">@drawable/drw_search_view_bg</item>
      <item name="searchHintIcon">@null</item>
      <item name="submitBackground">@null</item>
      <item name="closeIcon">@drawable/vd_cancel</item>
      <item name="searchIcon">@drawable/vd_search</item>
</style>
E Player Plus
  • 1,836
  • 16
  • 21