Search code examples
javaandroidsearchview

Android searchview remove hint padding


I'm working in my app and have a searchview inside toolbar but the hint has a small padding after the cursor.

How can I remove the padding?

Here is my searchview:

searchview

and I would like that:

searchview with no padding

here is my onCreateOptionsMenu:

SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    MenuItem item=menu.findItem(R.id.procura);
    Drawable drawable = menu.findItem(R.id.procura).getIcon();
    if (drawable != null) {
        drawable.mutate();
        drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
    }

    searchView=(SearchView) MenuItemCompat.getActionView(item);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setQueryHint(getResources().getString(R.string.app_hint));

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

My styles:

<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
    <item name="submitBackground">@color/colorPrimary</item>
    <item name="queryBackground">@color/colorPrimary</item>
    <item name="android:colorFocusedHighlight">@color/colorAccent</item>
</style>

My menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
    android:id="@+id/procura"
    android:title="@string/app_hint"
    android:icon="@mipmap/ic_search_black_24dp"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"/>


Solution

  • I found the solution for my problem.

    Just add this in the styles:

    <item name="searchHintIcon">@null</item>
    

    The styles now is that:

    <style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
        <item name="submitBackground">@color/colorPrimary</item>
        <item name="queryBackground">@color/colorPrimary</item>
        <item name="searchHintIcon">@null</item>
        <item name="android:colorFocusedHighlight">@color/colorAccent</item>
    </style>
    

    And onCreateOptionsMenu now is that:

    // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
    
    
        SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
        MenuItem item=menu.findItem(R.id.procura);
    
        searchView=(SearchView) MenuItemCompat.getActionView(item);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setQueryHint(getResources().getString(R.string.app_hint));
    
    
        return true;