SearchView Width + AutoComplete Dropdown Box

I am trying to set the ListView dropdown generated by AutoCompleteTextView to match_parent (full screen width)

This is what I got so far: enter image description here

I also use SearchView inspired by this answer here.

Here is the activity that inflates the menu

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_search, menu); MenuItem searchItem = menu.findItem(R.id.action_search); searchItem.expandActionView(); MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() { @Override public boolean onMenuItemActionExpand(MenuItem item) { return false; } @Override public boolean onMenuItemActionCollapse(MenuItem item) { onBackPressed(); return false; } }); final ArrayAdapterSearchView searchView = (ArrayAdapterSearchView) MenuItemCompat.getActionView(searchItem); searchView.setQueryHint(getResources().getString(R.string.location_autocomplete_hint)); searchView.setAdapter(mPlaceAdapter); searchView.setDropDownAnchor(R.id.anchor_dropdown); searchView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT); searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { searchView.setText(mPlaceAdapter.getItem(position).toString()); startSearch(position); } }); 

ArrayAdapter

 public class ArrayAdapterSearchView extends SearchView { private SearchView.SearchAutoComplete mSearchAutoComplete; public ArrayAdapterSearchView(Context context) { super(context); initialize(); } public ArrayAdapterSearchView(Context context, AttributeSet attrs) { super(context, attrs); initialize(); } public void initialize() { mSearchAutoComplete = (SearchAutoComplete) findViewById(android.support.v7.appcompat.R.id.search_src_text); this.setAdapter(null); this.setOnItemClickListener(null); } @Override public void setSuggestionsAdapter(CursorAdapter adapter) { // don't let anyone touch this } public void setOnItemClickListener(AdapterView.OnItemClickListener listener) { mSearchAutoComplete.setOnItemClickListener(listener); } public void setAdapter(ArrayAdapter<?> adapter) { mSearchAutoComplete.setAdapter(adapter); } public void setText(String text) { mSearchAutoComplete.setText(text); } public void setDropDownVerticalOffset(int value) { mSearchAutoComplete.setDropDownVerticalOffset(value); } public void setDropDownAnchor(int value) { mSearchAutoComplete.setDropDownAnchor(value); } public void setDropDownWidth (int value) { mSearchAutoComplete.setDropDownWidth(value); } public void setDropDownBackgroundResource(Drawable value) { mSearchAutoComplete.setDropDownBackgroundDrawable(value); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mSearchAutoComplete.getLayoutParams(); params.setMargins(0, 0, 0, 0); //substitute parameters for left, top, right, bottom mSearchAutoComplete.setLayoutParams(params); } } 

And finally, the menu_search.xml file

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_margin="0dp" android:padding="0dp" tools:context="com.example.activities.SearchActivity"> <item android:id="@+id/action_search" android:hint="@string/location_autocomplete_hint" android:title="@string/location_autocomplete_hint" android:icon="@android:drawable/ic_menu_search" app:showAsAction="collapseActionView" app:actionViewClass="com.example.adapters.ArrayAdapterSearchView" /> </menu> 

Xml main action

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/myWindowBackground" tools:context="com.example.activities.SearchActiviKty" android:orientation="vertical"> <include android:id="@+id/toolbar_actionbar" layout="@layout/toolbar_default" android:layout_width="match_parent" android:layout_height="wrap_content"/> <View android:id="@+id/anchor_dropdown" android:layout_width="fill_parent" android:layout_height="16dp"/> </LinearLayout> 

Finally, I use a simple TextView to populate the ListView.

My thoughts

I think SearchView has something to do with this problem, because if I replace SearchView with a simple AutoCompleteTextView, the dropdown list takes up the entire width of the screen.

Thanks!

+1
source share

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


All Articles