43

I am doing an application where for example when i click on the image(it is a searchView)

enter image description here

the search pad opens !

and it looks like enter image description here

but here the default search icon (magnifier) gets displayed but this dissappears as soon as some text is entered

enter image description here

but i dont want that magnifier to be displayed even the image is clicked for the first time

and here i am not using any xml file

my code is

public class MainActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout relative = new RelativeLayout(this);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        relative.setLayoutParams(params);
        setContentView(relative);

        SearchView searchView = new SearchView(this);
        traverseView(searchView, 0);
//      searchView.setIconifiedByDefault(false);
        LayoutParams searchViewparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//      searchViewparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        searchView.setLayoutParams(searchViewparams);
        relative.addView(searchView);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    private void traverseView(View view, int index) {
        if (view instanceof SearchView) {
            SearchView v = (SearchView) view;
            for(int i = 0; i < v.getChildCount(); i++) {
                traverseView(v.getChildAt(i), i);
            }
        } else if (view instanceof LinearLayout) {
            LinearLayout ll = (LinearLayout) view;
            for(int i = 0; i < ll.getChildCount(); i++) {
                traverseView(ll.getChildAt(i), i);
            }
        } else if (view instanceof EditText) {
            ((EditText) view).setTextColor(Color.GREEN);
            ((EditText) view).setHintTextColor(Color.BLACK);
        } else if (view instanceof TextView) {
            ((TextView) view).setTextColor(Color.BLUE);
        } else if (view instanceof ImageView) {
            ((ImageView) view).setImageResource(R.drawable.ic_launcher);
        } else {
            Log.v("View Scout", "Undefined view type here...");
        }
    }
}
mmBs
  • 8,421
  • 6
  • 38
  • 46
Hummer
  • 811
  • 1
  • 6
  • 14

16 Answers16

73

In my app i've used android.support.v7.widget.SearchView and to hide search icon this worked for me :

<android.support.v7.widget.SearchView
    ...
    app:iconifiedByDefault="false"
    app:searchIcon="@null"
/>
Armin
  • 2,397
  • 2
  • 21
  • 31
  • 1
    This works for me. The elongated discussion makes me worry about how compatible this solution is with different hardware, but it's by far the cleanest. – John Ward Feb 15 '17 at 19:14
  • 2
    Programatically is also possible: searchView.setIconifiedByDefault(false); – Bart Burg Sep 26 '17 at 12:26
33

I've had trouble with this too. I combined the tutorial I found and an existing answer found here in stackoverflow.

int magId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView magImage = (ImageView) searchView.findViewById(magId);
magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));

Take note that searchView is a LinearLayout, so use LinearLayout.LayoutParams to avoid an exception.

I also tried this but it doesn't remove the view. I can't seem to figure why.:

magImage.setVisibility(View.GONE);

For the other views that you need to change, refer to this tutorial.

chjarder
  • 614
  • 6
  • 10
  • 4
    I am getting NPE on `magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));` which means `magImage` is null, although I can't figure out why :-( – Solace Aug 07 '15 at 07:48
  • 1
    but in v7.SearchWidget we cannot hide searchhint icon by this method. Just do one thing where you get the instance of SearchView just check that you are not write searchView.setIconifiedByDefault(false); method – Deepak Gupta Sep 05 '15 at 06:25
  • 1
    For app compat [see](http://stackoverflow.com/questions/29633952/how-to-delete-or-change-the-searchview-icon-inside-the-searchview-actionbar/33229228#33229228) – Sarasranglt Oct 20 '15 at 06:11
  • 4
    Not writing `searchView.setIconifiedByDefault(false);` in addition to `setVisibility(View.GONE);` did the trick for me too. Weird behavior though. – dipdipdip Jan 18 '16 at 13:40
  • 13
    I used the android.support.v7.appcompat.SearchView. To solve hide it I Used: searchView.setIconifiedByDefault(false); ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon); searchIcon.setImageDrawable(null); – Roni Castro Jan 02 '17 at 01:01
32

I've simply put @null to searchHintIcon attribute:

<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
    <item name="searchHintIcon">@null</item>
</style>

and applied that style in my app theme

<style name="Theme.App" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
    <item name="searchViewStyle">@style/SearchViewMy</item>
</style>

Works for me.

Tomask
  • 2,344
  • 3
  • 27
  • 37
27

Try these three lines:

android:iconifiedByDefault="false"
android:searchIcon="@null"
android:searchHintIcon="@null"

*It works only for android.widget.SearchView

Silvio Guedes
  • 1,134
  • 15
  • 16
6

TL;DR: Simply setting the EditText's hint to the empty string is enough to remove the hint icon.

I found an answer that actually works. There's a lot of stuff on changing the icon using reflection - such as discussed at Styling the ActionBar SearchView. This website IS a great resource, but unfortunately the search hint ImageView does not change (even though the reflection does not cause errors). The only way that I was able to remove this image was this:

try {
    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
    searchPlate.setHint("");
}
catch (Throwable t)
{
    t.printStackTrace();
}

You should place this in your onCreateOptionsMenu method, and get a reference to your XML-declared SearchView using something like:

SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
Phil
  • 35,852
  • 23
  • 123
  • 164
4

I just met the same problem, here are some reference links.

http://blog.luxteam.net/2013/11/04/styling-appcompat-searchview/

https://android.googlesource.com/platform/frameworks/support.git/+/android-support-lib-19.1.0/v7/appcompat/src/android/support/v7/widget/SearchView.java

The key is in the following function, it sets the icon as an ImageSpan.

private CharSequence getDecoratedHint(CharSequence hintText) {
    // If the field is always expanded, then don't add the search icon to the hint
    if (!mIconifiedByDefault) return hintText;

    SpannableStringBuilder ssb = new SpannableStringBuilder("   "); // for the icon
    ssb.append(hintText);
    Drawable searchIcon = getContext().getResources().getDrawable(getSearchIconId());
    int textSize = (int) (mQueryTextView.getTextSize() * 1.25);
    searchIcon.setBounds(0, 0, textSize, textSize);
    ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return ssb;
}
Chao Zhang
  • 1,476
  • 2
  • 14
  • 25
4

Simple solution is searchHintIcon attribute

<style name="SearchViewStyleMyApp" parent="Widget.AppCompat.SearchView">
        <!-- Background for the search query section (e.g. EditText) -->
        <item name="queryBackground">@android:color/white</item>
        <!-- note that this is how you style your hint icon -->
        <item name="searchHintIcon">@null</item>
        <!-- The hint text that appears when the user has not typed anything -->
        <item name="queryHint">@string/search_hint</item>
    </style>
Rahul
  • 3,293
  • 2
  • 31
  • 43
4

For androidx you can do like this

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

It's work for me

Prototype
  • 486
  • 1
  • 4
  • 17
2

Use "searchHintIcon" attribute in your styles for that:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="searchViewStyle">@style/AppTheme.SearchView</item>
</style>

<style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
    <item name="searchHintIcon">@null</item>
</style>

You can replace @null with any drawable res you want

2

It's answer

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

ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);
roghayeh hosseini
  • 676
  • 1
  • 8
  • 21
  • 1
    in older android, when you set the searchIcon to null it still pushes the search text over quite a bit, as if the icon was still there taking up space. This answer works to remove that empty space – cmaroney Sep 10 '19 at 15:35
1

Simply set the searchHintIcon property as null in your Searchview.

<androidx.appcompat.widget.SearchView
app:searchHintIcon="@null" />

Aishwarya
  • 151
  • 1
  • 5
0

you can try this :

  public class MainActivity extends Activity {

        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            RelativeLayout relative = new RelativeLayout(this);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
            relative.setLayoutParams(params);
            setContentView(relative);

            SearchView searchView = new SearchView(this);
searchView.setIconifiedByDefault(false);
            traverseView(searchView, 0);
    //      searchView.setIconifiedByDefault(false);
            LayoutParams searchViewparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    //      searchViewparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            searchView.setLayoutParams(searchViewparams);
            relative.addView(searchView);
        }
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @SuppressLint("NewApi")
        private void traverseView(View view, int index) {
            if (view instanceof SearchView) {
                SearchView v = (SearchView) view;
                for(int i = 0; i < v.getChildCount(); i++) {
                    traverseView(v.getChildAt(i), i);
                }
            } else if (view instanceof LinearLayout) {
                LinearLayout ll = (LinearLayout) view;
                for(int i = 0; i < ll.getChildCount(); i++) {
                    traverseView(ll.getChildAt(i), i);
                }
            } else if (view instanceof EditText) {
                ((EditText) view).setTextColor(Color.GREEN);
                ((EditText) view).setHintTextColor(Color.BLACK); 
            } else if (view instanceof TextView) {
                ((TextView) view).setTextColor(Color.BLUE);
            } else if (view instanceof ImageView) {
                ((ImageView) view).setImageResource(R.drawable.ic_launcher);
            } else {
                Log.v("View Scout", "Undefined view type here...");
            }
        }
    }
Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
  • sorry buddy i am not getting you the above is the whole code which i wrote and please just post a comment with the code adding the above lines please – Hummer Dec 02 '13 at 09:16
  • yaar just Seticonifiedbydefault karee toh bhi yehi procedure aa jayega but i just want to remove that magnifier i mean i should not get that hint at any cost – Hummer Dec 02 '13 at 09:46
  • yes, Seticonifiedbydefault remove that default icon which you need. – Harshit Rathi Dec 02 '13 at 09:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42312/discussion-between-hummer-and-harshit-rathi) – Hummer Dec 02 '13 at 09:50
0

with v7 Widget , if you use searchView.setIconifiedByDefault(false) the beahvior of the search view differs . You only set hint icon to @null in xml. Also you can apply this manner for other icons.

maniaq
  • 185
  • 1
  • 11
0

androidx.appcompat.widget.SearchView

If you use androidx.appcompat.widget.SearchView:

You can hide the icon using this:

val magImage = searchView.findViewById<View>(androidx.appcompat.R.id.search_mag_icon) as ImageView
magImage.layoutParams = LinearLayout.LayoutParams(0, 0)
Aydın Ahmed
  • 559
  • 5
  • 11
0

I know it`s ugly, but it only works for me:

MenuItem menuSearch = menu.findItem(R.id.menu_search);
mSearchView = (SearchView) menuSearch.getActionView();

SearchView.SearchAutoComplete searchAutoComplete = mSearchView.findViewById(androidx.appcompat.R.id.search_src_text);

Class<?> clazz = Class.forName("androidx.appcompat.widget.SearchView$SearchAutoComplete");
SpannableStringBuilder stopHint = new SpannableStringBuilder("");
stopHint.append(getString(R.string.search_hint));
Method setHintMethod = clazz.getMethod("setHint", CharSequence.class);
setHintMethod.invoke(searchAutoComplete, stopHint);
MLDZ
  • 166
  • 1
  • 6
0

That's an old question, but this helped me a lot in my case: If you are trying to hide inside a menu, none of the questions posted here worked for me. What solved the problem was: Instead of putting

app:actionViewClass="android.widget.SearchView"

inside the item in menu, you can change for:

app:actionLayout="@layout/search_layout"

And, inside this layout, you can add this piece of code:

<?xml version="1.0" encoding="utf-8"?>
<android.widget.SearchView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:searchHintIcon="@drawable/nothing_drawed"/>

And, finally, inside the drawable you just create it with nothing inside, example:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

</selector>

Only this method worked for me. When I tried to hide the searchHintIcon, setting to @null or anything else, it remained the same thing as if I had written no code.