Removing default search icon from SearchView widget

删除回忆录丶 提交于 2019-12-10 21:16:44

问题


How can i remove default search icon which appears as a hint in SearchView widget(not in actionbar)?Through styles i can change the icon but i can't find a way to remove it?


回答1:


It's Worked......

ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);

ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);



回答2:


For me it only worked this:

ImageView magImage = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);
magImage.setImageDrawable(null);

@sector11's almost got it, but still you need to add the line magImage.setImageDrawable(null) because taking a look at SearchView.java in Github,specifically the method updateViewsVisibility, you can see that it constantly gets its visibility updated

Extract from GitHub

if (mCollapsedIcon.getDrawable() == null || mIconifiedByDefault) {
    iconVisibility = GONE;
} else {
    iconVisibility = VISIBLE;
}
mCollapsedIcon.setVisibility(iconVisibility);



回答3:


As of now you should use

ImageView magImage = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);



回答4:


you can override it:

    int searchImgId = context.getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView searchImage = (ImageView) searchView.findViewById(searchImgId);


searchImage.setVisibility(View.GONE); 

If your search view is in layout then please use EditText with close button:

https://github.com/yanchenko/droidparts/blob/develop/droidparts/src/org/droidparts/widget/ClearableEditText.java

If you want to create your own then use frame layout and add text change listener to mange visibility of close or cancel button.

there are few discussion regarding search view in UI:

How to create EditText with cross(x) button at end of it?

How to place button inside of edit text

If you want to show search list then use AutoCompleteTextView instead of EditText.

you can read tutorial from below link:

  1. http://www.tutorialspoint.com/android/android_auto_complete.htm

2.http://examples.javacodegeeks.com/android/core/widget/autocompletetextview/android-auto-complete-example/

3.http://androidexample.com/Show_AutoComplete_Suggestions_-_Android_Example/index.php?view=article_discription&aid=105&aaid=127

I hope it will work.




回答5:


For androidx SearchView just use app:searchIcon="@null"




回答6:


Try this one:

<style name="SearchViewStyle"  parent="Widget.AppCompat.SearchView" >
    <item name="searchHintIcon">@null</item>
     <!-- Background for the search query section (e.g. EditText) -->
    <item name="queryBackground">@color/transparent</item>
</style>

Never use direct android ids to find the view because in future if the ids change, your code will not work. Always use standard ways to solve the problems instead of a workaround. for more info refer this link.



来源:https://stackoverflow.com/questions/29530742/removing-default-search-icon-from-searchview-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!