Searchview in Optionsmenu Not Full Width

SearchView in OptionsMenu not full width

I had a similar issue, that the search bar did not fill the whole width,( but I had no other icons).
I solved it by adding in item:

android:actionLayout="@layout/my_search_view"

and in layout/my_search_view.xml :

<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

SearchView doesn't expand full width

MenuItemCompat's SearchView has a property named maxWidth. The following does the trick:

searchView.setMaxWidth( Integer.MAX_VALUE );

SearchView in ActionBar not full width

put it in your code :

searchView.setMaxWidth(Integer.MAX_VALUE);

How can I remove the left space between back button and SearchView

By default, Toolbar have 16dp inset after backbutton. So, include app:contentInsetStartWithNavigation="0dp" in Toolbar, it will remove that whitespace.

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
android:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:contentInsetStart="0dp">

How to set expanded ActionView's width in SupportActionBar to fill the available space

The "magic" takes place in Toolbar.expandItemActionView. This method overrides the layout parameters of the expanded ActionView and sets the width to wrap_content. Now to one possible solution:

Toolbar.expandItemActionView checks if the ActionView implements CollapsibleActionView interface and calls CollapsibleActionView.onActionViewExpanded method if this is the case. This is your chance to fix the layout paramters and set the width to match_parent. Derive a custom class from the SeekBar and let it implement CollapsibleActionView:

public class CollapsibleSeekBar extends AppCompatSeekBar implements CollapsibleActionView {
public CollapsibleSeekBar(Context context) {
super(context);
}

public CollapsibleSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CollapsibleSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
public void onActionViewExpanded() {
ViewGroup.LayoutParams params = getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
setLayoutParams(params);
requestLayout();
}

@Override
public void onActionViewCollapsed() {

}
}

Use it in your menu item via app:actionViewClass.

Also make sure to use the appropriate CollapsibleActionView.

If you are using android.support.v4.widget.Toolbar you also need android.support.v7.view.CollapsibleActionView.

If you have an android.widget.Toolbar use android.view.CollapsibleActionView



Related Topics



Leave a reply



Submit