1

How can I change or remove the magnifier icon of the EditText input in an android SearchView?

Magnifier icon of android edittext search view

SOLUTION ====

Its an ImageSpan which is set only in the hint text.

Simply do:

int id = resources.getIdentifier("search_src_text", "id", "android");
View autoComplete = searchView.findViewById(id);
autoComplete.setHint(R.string.what_you_like) // or a custom span
sonix
  • 243
  • 2
  • 17
  • I think this will help you solve the problem. [Click !][1] [1]: http://stackoverflow.com/questions/20323990/remove-the-searchicon-as-hint-in-the-searchview – Boris Pawlowski Sep 17 '14 at 16:01

1 Answers1

0

I managed to do it this way in my application (to change the icon, but you can probably adapt it to hide it). If someone has an easier way, I'll be glad to hear it (like in XML for example).

    try {
        Resources resources = this.getResources();
        int id = resources.getIdentifier("search_src_text", "id", "android");
        View autoComplete = searchView.findViewById(id);
        Class<?> clazz = Class.forName("android.widget.SearchView$SearchAutoComplete");
        Method textSizeMethod = clazz.getMethod("getTextSize");
        Float rawTextSize = (Float) textSizeMethod.invoke(autoComplete);
        int textSize = (int) (rawTextSize * 1.25);

        Drawable searchIcon = resources.getDrawable(R.drawable.ic_action_search);
        searchIcon.setBounds(0, 0, textSize, textSize);

        SpannableStringBuilder stopHint = new SpannableStringBuilder("   ");
        stopHint.append(this.getString(R.string.search_quotes));
        stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        Method setHintMethod = clazz.getMethod("setHint", CharSequence.class);
        setHintMethod.invoke(autoComplete, stopHint);
    }
    catch (Exception exception) {
        exception.printStackTace();
    }

From How to style the ActionBar SearchView programmatically.

Gaëtan
  • 11,912
  • 7
  • 35
  • 45