Remove the search preview icon from the tooltip text.

I am using the appcompat library with a toolbar and I have implemented a search view in my application. But I want to remove the search icon when it is expanded.

here is my xml file menu:

<item android:id="@+id/action_search" android:icon="@drawable/ic_search" android:title="@string/action_search" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="ifRoom|collapseActionView"/> 

and this is how I inflate the menu:

 getMenuInflater().inflate(R.menu.main, menu); SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); searchView.setQueryHint(getString(R.string.search_hint)); return super.onCreateOptionsMenu(menu); 

this is what i want to remove:

enter image description here

+7
android android-actionbar searchview appcompat android-actionbar-compat
source share
7 answers

This helped me remove the search icon.

 searchView.setIconified(false); 

If you want searchView to always expand, use only this:

 searchView.onActionViewExpanded(); 
+17
source share

Try the following:

In the application theme, add the following line:

 <item name="searchViewStyle">@style/MySearchViewStyle</item> 

Add a style definition below:

 <style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView"> <item name="searchHintIcon">@null</item> </style> 
+7
source share

In your onCreateOptionsMenu

 int searchPlateId = searchView.getContext().getResources() .getIdentifier("android:id/search_src_text", null, null); EditText searchPlate = (EditText) searchView .findViewById(searchPlateId); searchPlate.setHint(""); 
+2
source share
 try { Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon"); mDrawable.setAccessible(true); Drawable drawable = (Drawable) mDrawable.get(searchView); drawable.setBounds(0, 0, 0, 0); } catch (Exception e) { e.printStackTrace(); } 

setBounds does the magic

+2
source share

If you look at the Android source code for the SearchView layout, you will see that the search icon has the identifier search_mag_icon. So, in onCreateOptionsMenu,

  @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_search,menu); MenuItem searchViewItem = menu.findItem(R.id.search); SearchView searchView = (SearchView)searchViewItem.getActionView(); searchView.findViewById(R.id.search_mag_icon).setVisibility(View.GONE); } 

Just find the view by id and set its visibility for deletion.

+1
source share

The easiest way is Here ... In the case of AppCompat

 app:iconifiedByDefault="false" app:searchIcon="@null" 

OR otherwise

 android:iconifiedByDefault="false" android:searchIcon="@null" 
+1
source share

See SumeetP for this question . Not sure if this works because I never used it. It gets the search icon as ImageView. So I think you can just set the image to android.R.color.transparent so that it is not visible.

0
source share

All Articles