Situation:
I have a searchview widget in my appcompat toolbar that allows custom suggestions via sqlitedatabase.
Problem:
I am having trouble expanding the drop down suggestions list to be the full width of the screen. At best, the list width is almost the width of the screen except for small margin/padding problems on the left and right side. How do I make the drop down list have the same width as the screen?
Things I've tried:
SearchView results list width
AutoCompleteText's setPadding, setRight/setLeft, setDropDownHorizontalOffset, setting layout params, and setting width to be 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);
}
});
}