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:
Any ideas why is that happening?
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With