Android Developer
Android Developer

Reputation: 9653

Removing default search icon from SearchView widget

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? enter image description here

Upvotes: 5

Views: 13252

Answers (7)

Vinod Prajapati
Vinod Prajapati

Reputation: 21

please set this SearchView in your code

android:paddingLeft="-16dp" // this line is remove the space of icon

android:paddingStart="-16dp" // // this line is remove the space of icon

android:searchIcon="@null" // this line to remove the search icon

android:closeIcon="@null" // this line to remove the close icon

 <SearchView
                        android:queryBackground="@android:color/transparent"
                        android:id="@+id/search"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:paddingLeft="-16dp"
                        android:paddingStart="-16dp"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:background="@null"
                        android:searchIcon="@null"
                        android:closeIcon="@null"
                        android:theme="@style/BaseTheme"
                        android:iconifiedByDefault="false"
                        android:queryHint="@string/hint">

                        <requestFocus />

                    </SearchView>

Upvotes: 2

Mark
Mark

Reputation: 3474

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

Upvotes: 4

Anand Soni
Anand Soni

Reputation: 186

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.

Upvotes: 3

I.G. Pascual
I.G. Pascual

Reputation: 5995

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);

Upvotes: 5

Praful Parmar
Praful Parmar

Reputation: 217

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);

Upvotes: 10

silentsudo
silentsudo

Reputation: 6973

As of now you should use

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

Upvotes: 3

Yogendra
Yogendra

Reputation: 5288

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.

Upvotes: 2

Related Questions