SearchView does not fill the entire width

I add SearchView to the toolbar via the 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 moves to the right: enter image description here

Any ideas why this is happening?

+5
source share
2 answers

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

 searchView.setMaxWidth( Integer.MAX_VALUE ); 

If that does not work, try replacing the tag <item> to <SearchView>

If you use the API 20 or higher, do it
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> different - depends on the one that already /*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> application> theme: AppTheme * / /*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> = "Theme.AppCompat.Light.DarkActionBar"> / /*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> "> @ dimen / maxSize </ item> /*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> 
+1
source

I had the same problem, but in my case it happened that on the toolbar (in the layout file) I got an image of an icon in the menu, and at the same time the "home" button was programmatically similar

getSupportActionBar().setDisplayHomeAsUpEnabled(true)

By setting this to false (or removing this code), free up space for the drawer icon and SearchView. Hope this helps someone.

0
source

All Articles