Remove the SearchIcon as hint in the searchView

2019-01-11 16:31发布

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...");
        }
    }
}

9条回答
beautiful°
2楼-- · 2019-01-11 17:06

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.

查看更多
冷血范
3楼-- · 2019-01-11 17:14

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"
/>
查看更多
老娘就宠你
4楼-- · 2019-01-11 17:19

Try these three lines:

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

*It works only for android.widget.SearchView

查看更多
男人必须洒脱
5楼-- · 2019-01-11 17:20

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();
查看更多
叼着烟拽天下
6楼-- · 2019-01-11 17:20

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;
}
查看更多
老娘就宠你
7楼-- · 2019-01-11 17:24

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...");
            }
        }
    }
查看更多
登录 后发表回答