When using the action bar search interface, the widget expands to occupy the full width of the screen in portrait mode, but stops short in landscape mode.
Is there a way to set the expansion layout params on the SearchView to fully fill the action bar when the user is typing a search?
See image:
Note: I'm not currently using ActionBar Sherlock
Edit: Here's what I did to get it to extend the full width.
searchView.setOnSearchClickListener(new OnClickListener() {
private boolean extended = false;
@Override
public void onClick(View v) {
if (!extended) {
extended = true;
LayoutParams lp = v.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
}
}
});
Use this (100%):
I figured out a solution for fully expanding the search view in landscape and to also have the action view already expanded when the activity is created. Here how it works:
1.First create an xml file in your res-menu folder called for example : searchview_in_menu.xml. Here you would have the following code:
Note: "@string/search" - looks something like this in the res-strings.xml:
2.Second create the layout referred above ("@layout/searchview_layout") in res-layout folder. The new layout: searchview_layout.xml will look like this:
Note: Here we are setting the search view width to match the width of its parent( android:layout_width="match_parent")
3.In your MainActivity class or in the activity that has to implement the Search View write in the onCreateOptionsMenu() method the following code:
For ActionBarCompat use this:
Adjust the width layout param to
MATCH_PARENT
, that way theSearchView
will fully expand in either portrait or landscapeDid you try something like that?
MenuItemCompat's SearchView has a property named maxWidth.
use screen width instead of xxx offcourse