How to display SearchView suggestions which use the whole screen width using appcompat-v7:21
?
I use android.support.v7.widget.SearchView in code and the menu-resource. The new Toolbar widget has a searchViewStyle
, but I couldn't find a parameter to display suggestions full width (match_parent).
(source: netdna-cdn.com)
you can do it like this: (it's on DropDownAnchor)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
int searchEditTextId = R.id.search_src_text;
final AutoCompleteTextView searchEditText = (AutoCompleteTextView) searchView.findViewById(searchEditTextId);
final View dropDownAnchor = searchView.findViewById(searchEditText.getDropDownAnchor());
if (dropDownAnchor != null) {
dropDownAnchor.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
// screen width
int screenWidthPixel = ActivitySearchUni.this.getResources().getDisplayMetrics().widthPixels;
searchEditText.setDropDownWidth(screenWidthPixel);
}
});
}
return true;
}
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