Android SearchView menu screen encryption

Situation:

I have a search widget on the appompat toolbar that allows me to create my own sentences through sqlitedatabase.

Problem:

I'm having trouble expanding the dropdown list to get the full screen width. In the best case, the width of the list is almost the width of the screen, with the exception of minor problems with stock / stock left and right. How to make a drop-down list the same width as the screen?

What I tried:

Search results list width

AutoCompleteText setPadding, setRight / setLeft, setDropDownHorizontalOffset, layout settings and installation width - match_parent.

menu / search.xml

<item android:id="@+id/search_restaurant" android:title="Search" android:icon="@drawable/search" android:orderInCategory="2" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="always"/> 

Res / search_view_suggestions_list_item

 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="48dp" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:background="@android:color/white" android:gravity="center_vertical" android:paddingLeft="16dp" android:paddingRight="16dp" android:minHeight="?android:attr/listPreferredItemHeightSmall"/> 

activity / Activity.java

  MenuItem searchItem = menu.findItem(R.id.search_restaurant); searchView = (SearchView) MenuItemCompat.getActionView(searchItem); searchView.setQueryHint("Search Restaurant"); searchView.setOnQueryTextListener(this); searchView.setOnSuggestionListener(this); final AutoCompleteTextView searchAutoCompleteTextView = (AutoCompleteTextView) searchView .findViewById(R.id.search_src_text); searchAutoCompleteTextView.setThreshold(1); searchAutoCompleteTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT); // Tried to change margins/padding here LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) searchAutoCompleteTextView.getLayoutParams(); params.setMargins(0, 0, 0, 0); searchAutoCompleteTextView.setLayoutParams(params); searchAutoCompleteTextView.setPadding(0, 0, 0, 0); searchAutoCompleteTextView.setDropDownHorizontalOffset(-100); searchAutoCompleteTextView.setPadding(0,0,0,0); searchAutoCompleteTextView.setLeft(0); final View dropDownSuggestions = searchView.findViewById(searchAutoCompleteTextView .getDropDownAnchor()); if (dropDownSuggestions != null) { dropDownSuggestions.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) { int point[] = new int[2]; dropDownSuggestions.getLocationOnScreen(point); int dropDownPadding = point[0] + searchAutoCompleteTextView .getDropDownHorizontalOffset(); Rect screenSize = new Rect(); getWindowManager().getDefaultDisplay().getRectSize(screenSize); int screenWidth = screenSize.width(); searchAutoCompleteTextView.setDropDownWidth(screenWidth * 2); } }); } 
+1
source share
2 answers

The size of the width you want can be achieved using Wrap content. Try this code: searchAutoCompleteTextView.setDropDownWidth (ViewGroup.LayoutParams.WRAP_CONTENT);

I came up with this answer when I looked at this link:

https://www.youtube.com/watch?v=408pMAQFnvs

+2
source

You should get a link to the SearchAutoComplete object that uses SearchView, and then set its size and change the background resource with additions, please refer to my answer here:

fooobar.com/questions/976569 / ...

0
source

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


All Articles