Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView Suggestion - Layout width: match_parent

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).

Example
(source: netdna-cdn.com)

like image 280
Paradiesstaub Avatar asked Jan 14 '15 15:01

Paradiesstaub


1 Answers

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;
}
like image 185
Ali_Ai_Dev Avatar answered Sep 29 '22 11:09

Ali_Ai_Dev