Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView doesn't fill the entire width

I'm adding SearchView to my Toolbar by menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search_white"
        android:orderInCategory="0"
        android:title="@android:string/search_go"
        android:visible="true"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:iconifiedByDefault="false"
        app:showAsAction="always" />
</menu>

I don't know why but SearchView is moved to the right: enter image description here

Any ideas why is that happening?

like image 478
Dawid Hyży Avatar asked Jul 31 '16 09:07

Dawid Hyży


2 Answers

I had the same problem, try to add this programmatically:

searchView.setMaxWidth( Integer.MAX_VALUE );

If this doesn't work try to replace the <item> tag to <SearchView>

If using API 20 and above do this
Add this to your styles.xml file:

/*Theme name and parent can be different - depends on the one that already 
declared in you manifest under the tag <application> theme:AppTheme*/
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> /
    <item name="searchViewStyle">@style/SearchView</item>
</style>

<style name="SearchView" parent="Widget.AppCompat.SearchView">
    <item name="android:maxWidth">@dimen/maxSize</item>
</style>
<dimen name="maxSize">1000dp</dimen>
like image 56
Nir Duan Avatar answered Oct 02 '22 16:10

Nir Duan


I had the same problem but in my case, it happened that I had a menu drawer icon image in the toolbar (in the layout file) and at the same time had enabled the home button programmatically like so

getSupportActionBar().setDisplayHomeAsUpEnabled(true)

Setting this as false (or removing this code) freed up space for the drawer icon and the SearchView. Hope this helps someone.

like image 37
tfad334 Avatar answered Oct 02 '22 15:10

tfad334