1

I want to implement searchview with suggestions-list. Normally,suggestions-list dropdown width appears as same as the searchview text area.But when I set the following property in AndroidManifest.xml android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" to application tag, I see that suggestions-list width appears greater than the searchview text area. I tried setting dropdownwidth with autoCompleteTextView.setDropdownWidth(int)to some value, did not work.

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kavya
  • 11
  • 2

1 Answers1

0

You can do something like this, here I needed to fill the screen width with dropdown:

@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();


final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

    // get AutoCompleteTextView from SearchView
    final AutoCompleteTextView searchEditText = (AutoCompleteTextView) searchView.findViewById(searchEditTextId);
    final View dropDownAnchor = findViewById(searchTextView.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] + searchTextView.getDropDownHorizontalOffset();

                Rect screenSize = new Rect();
                getWindowManager().getDefaultDisplay().getRectSize(screenSize);
                // screen width
                int screenWidth = screenSize.width();

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

    return super.onCreateOptionsMenu(menu);
}

Original answer: https://stackoverflow.com/a/26344053/1959110

galaxigirl
  • 2,390
  • 2
  • 20
  • 29
dumb_terminal
  • 1,815
  • 1
  • 16
  • 27