Skip to content Skip to sidebar Skip to footer

Searchview Results List Width

Is there a way to make SearchView's results list occupy whole screen width? I've tried using custom layout for SearchView, but this doesn't change the search results list's width.

Solution 1:

Yes, it is possible. To achieve this we should set width of whole DropDownView, not only for items. We can set it by calling setDropDownWidth of AutoCompleteTextView. But there is one problem. DropDownView's width and horizontal offset are calculated inside SearchView each time its bounds is changed. To get a workaround we can add to SearchView our own OnLayoutChangeListener where we calculate and set the height of DropDownView. The following code is fully working with AppCompat:

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    // inflate our menu
    getMenuInflater().inflate(R.menu.search_menu, menu);

    // find MenuItem and get SearchView from itMenuItemsearchMenuItem= menu.findItem(R.id.search);
    SearchViewsearchView= (SearchView) searchMenuItem.getActionView();

    // id of AutoCompleteTextViewintsearchEditTextId= R.id.search_src_text; // for AppCompat// get AutoCompleteTextView from SearchViewfinalAutoCompleteTextViewsearchEditText= (AutoCompleteTextView) searchView.findViewById(searchEditTextId);
    finalViewdropDownAnchor= searchView.findViewById(searchEditText.getDropDownAnchor());
    if (dropDownAnchor != null) {
        dropDownAnchor.addOnLayoutChangeListener(newView.OnLayoutChangeListener() {
            @OverridepublicvoidonLayoutChange(View v, int left, int top, int right, int bottom,
                                       int oldLeft, int oldTop, int oldRight, int oldBottom) {

                // calculate width of DropdownViewint point[] = newint[2];
                dropDownAnchor.getLocationOnScreen(point);
                // x coordinate of DropDownViewintdropDownPadding= point[0] + searchEditText.getDropDownHorizontalOffset();

                RectscreenSize=newRect();
                getWindowManager().getDefaultDisplay().getRectSize(screenSize);
                // screen widthintscreenWidth= screenSize.width();

                // set DropDownView width
                searchEditText.setDropDownWidth(screenWidth - dropDownPadding * 2);
            }
        });
    }

    returnsuper.onCreateOptionsMenu(menu);
}

If you use Holo just change the line int searchEditTextId = R.id.search_src_text; to the below one:

int searchEditTextId = getResources().getIdentifier("android:id/search_src_text", null, null);

Solution 2:

You must get reference to SearchAutoComplete object which in my case android.support.v7.widget.SearchView using , then set its size , please check my answer here :

https://stackoverflow.com/a/43480908/5255624

Post a Comment for "Searchview Results List Width"