Search code examples
javaandroidsearchviewandroid-search

How to programmatically eliminate space occupied by SearchView hint icon in SearchView


I'm trying to programmatically remove space that the icon within the SearchView hint takes up, but for some reason it won't go away.

This code makes the icon invisible, but it doesn't remove the space:

    try {
        Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
        mDrawable.setAccessible(true);
        Drawable drawable =  (Drawable)mDrawable.get(searchView);
        drawable.setAlpha(0);
    } catch (Exception e) {
        e.printStackTrace();
    }

enter image description here


Solution

  • Add this line:

    drawable.setBounds(0,0,0,0);
    

    to your code because the drawable is still there even if it is invisible