Search Results List Width

Is there a way for the SearchView result list to fill the entire width of the screen? I tried using a custom layout for SearchView, but that does not change the width of the list of search results. See screenshot:

enter image description here

+6
source share
2 answers

Yes it is possible. To do this, we must set the width of the entire DropDownView , and not just for the elements. We can set it by calling setDropDownWidth of AutoCompleteTextView . But there is one problem. DropDownView width and horizontal offset are calculated inside the SearchView every time its borders are changed. To get a workaround, we can add SearchView our own OnLayoutChangeListener , where we calculate and set the height of the DropDownView . The following code works fully with AppCompat :

 @Override public boolean onCreateOptionsMenu(Menu menu) { // inflate our menu getMenuInflater().inflate(R.menu.search_menu, menu); // find MenuItem and get SearchView from it MenuItem searchMenuItem = menu.findItem(R.id.search); SearchView searchView = (SearchView) searchMenuItem.getActionView(); // id of AutoCompleteTextView int searchEditTextId = R.id.search_src_text; // for AppCompat // get AutoCompleteTextView from SearchView 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) { // calculate width of DropdownView int point[] = new int[2]; dropDownAnchor.getLocationOnScreen(point); // x coordinate of DropDownView int dropDownPadding = point[0] + searchEditText.getDropDownHorizontalOffset(); Rect screenSize = new Rect(); getWindowManager().getDefaultDisplay().getRectSize(screenSize); // screen width int screenWidth = screenSize.width(); // set DropDownView width searchEditText.setDropDownWidth(screenWidth - dropDownPadding * 2); } }); } return super.onCreateOptionsMenu(menu); } 

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

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

You should get a link to the SearchAutoComplete object, which in my case uses android.support.v7.widget.SearchView, and then set its size, please check my answer here:

fooobar.com/questions/976565 / ...

-1
source

Source: https://habr.com/ru/post/976563/


All Articles