How to remove or change the search view icon inside the edittext? I am using the Appcompat libra
remove is the right drawable that you have mentioned in xml:
final Drawable remove = ContextCompat.getDrawable(getActivity(), R.drawable.ic_close_circle);
etSearchProducts.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (etSearchProducts.getCompoundDrawables()[2] == null) {
return false;
}
if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
if (event.getX() > etSearchProducts.getWidth() - etSearchProducts.getPaddingRight() - remove.getIntrinsicWidth()) {
etSearchProducts.setText("");
Utility.hideKeyboard(getActivity());
}
return false;
}
});
I used the menu item with the property app:actionViewClass="android.support.v7.widget.SearchView"
On the activity I set:
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setIconifiedByDefault(false);
To remove the icon I did:
ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
searchIcon.setImageDrawable(null);
no a good idea use private method as SearchView.class.getDeclaredField("mSearchHintIcon");
<item android:id="@+id/menu_search"
android:icon="@drawable/action_search"
app:actionLayout="@layout/xx_layout"
app:showAsAction="always"/>
and in xx_layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:searchHintIcon="@null"
android:iconifiedByDefault="true" />
Finally found the solution. Try the following:
try {
Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
mDrawable.setAccessible(true);
Drawable drawable = (Drawable)mDrawable.get(your search view here);
drawable.setAlpha(0);
} catch (Exception e) {
e.printStackTrace();
}
Simplest way is
In case you are using AppCompat
use
app:iconifiedByDefault="false"
app:searchIcon="@null"
Else use android
in place of app
in above code as
android:iconifiedByDefault="false"
android:searchIcon="@null"
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!