I know there's already a lot of answers for this question here, and I wouldn't be creating another thread if I hadn't already tried everything I saw here. Anyway, the question is pretty simple. I don't mind if the solution comes from XML styling or Java code (both solutions are welcome). I will show everything (most of it anyway) I've tried to solve it.
First of all, the searview icon seems to be immutable by code since I can't change anything from it. My searchview is declared in menu.xml
as this
<item android:id="@+id/icon_search"
android:title="Searchador"
android:icon="@drawable/ic_search_black_24dp" <!-- icon not used by searchview anyway -->
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
/>
and inside my onCreateOptionsMenu
I have this
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.icon_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(false);
searchView.setOnQueryTextListener(this);
MenuTint.colorIcons(this, menu, Color.BLUE);
return true;
The MenuTint.colorIcons(this, menu, Color.BLUE);
I got from a special class I found here where it basically changes all my icons color, unfortunately not working for the searchview icon.
I then found some answers suggesting to use a style like this
<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
<!-- changing my icon, its color and stuff.. -->
<item name="searchIcon">@drawable/ic_search</item></style>
but I was getting the No resource found that matches the given name 'Widget.AppCompat.SearchView'.
error.
I then found that I should add the android-support-app-compat
project library instead of just the .jar
. It didn't work. I mean, the import worked, but the error kept showing. Then, I found somewhere that I should change the project.properties
from target=android-19 to 21 (or higher) but it didn't solved the "No resource found" error.
So I'm pretty much stuck for this simple detail where I need to change the searchview color.
Also, this is not exactly the same question, but I believe it might be solved the in the same way so I will include here: I wanted to change distance between the icons. Someone suggested this solution
<style name="ActionButtonStyle" parent="AppBaseTheme">
<item name="android:minWidth">0dip</item>
<item name="android:paddingLeft">0dip</item>
<item name="android:paddingRight">0dip</item>
</style>
but I end up getting this
As you can see, this approach works for my custom icons only, but not for the searchview icon, neither for the menu overflow icon. I would like to change the distance between ALL icons equally.
You can change or customize the icons of the SearchView by adding you're own drawable icons.
<androidx.appcompat.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:closeIcon="@drawable/close_icon"
app:searchHintIcon="@drawable/search_icon"
app:searchIcon="@drawable/search_icon"
android:textStyle="bold" />
Use below code:
ImageView searchIcon = searchView.findViewById(R.id.search_button);
searchIcon.setColorFilter(Color.WHITE);
If you want to change close icon color, use this one:
ImageView searchClose = searchView.findViewById(R.id.search_close_btn);
searchIcon.setColorFilter(Color.WHITE);
final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchViewItem);
// change close icon color
ImageView iconClose = (ImageView) searchViewAndroidActionBar.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
iconClose.setColorFilter(getResources().getColor(R.color.grey));
//change search icon color
ImageView iconSearch = searchViewAndroidActionBar.findViewById(android.support.v7.appcompat.R.id.search_button);
iconSearch.setColorFilter(getResources().getColor(R.color.grey));
If you want to change SearchView's search icon, you just need to get the image view within the view as follow:
searchView = v.findViewById(R.id.searchView);
//change icon color
ImageView searchIcon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchIcon.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_search_icon));
It is important that the icon is R.id.search_button and you can replace it with a white vector asset that you provide, in this case R.drawable.ic_search_icon
Similarly, you can change the text within the SearchView as follows:
//set color to searchview
SearchView.SearchAutoComplete searchAutoComplete = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(getResources().getColorandroid.R.color.white));
searchAutoComplete.setTextColor(getResources().getColor(android.R.color.white));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With