Reputation: 258
I want to display a fully expanded SearchView for particular activities in my application, however, the SearchView only spans half the ActionBar for these activities.
I'm currently using the v7 support library.
Here's the code for my SearchView in menu_home.xml:
<menu...
<item
android:id="@+id/action_search"
android:title="search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView" /
</menu>
Here's my Activity code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
if (showSearch) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
searchView.setIconifiedByDefault(false);
} else {
item.setVisible(false);
getSupportActionBar().setDisplayShowTitleEnabled(true);
}
return true;
}
And here's what the SearchView looks like:
I've tried setting the SearchView LayoutParams.width to LayoutParams.MATCH_PARENT, but SearchView.getLayoutParams() kept returning null. I tried ViewGroup.LayoutParams, LinearLayout.LayoutParams, ActionBar.LayoutParams, and v7.ActionBar.LayoutParams.
If it's any help, this activity is exclusively in Portrait mode.
Upvotes: 3
Views: 923
Reputation: 993
MenuItemCompat
's SearchView
has a property named maxWidth
.
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setMaxWidth(xxx);
Use screen width instead of xxx
of course
Upvotes: 4