0

I have a problem with my searchview. I'm trying to make search suggestions full width but the suggestions have a right padding.

my app searchview

<-- language: lang-java -->

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);


    SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    final MenuItem item=menu.findItem(R.id.procura);

    MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            tbs.setVisibility(View.GONE);
            vwpgr.setVisibility(View.GONE);
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            tbs.setVisibility(View.VISIBLE);
            vwpgr.setVisibility(View.VISIBLE);
            return true;
        }
    });

    searchView=(SearchView) MenuItemCompat.getActionView(item);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setQueryHint(getResources().getString(R.string.app_hint));
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Intent search=new Intent(MainActivity.this,searchableactivity.class);
            search.putExtra("searchquery",query);
            startActivity(search);
            searchView.clearFocus();
            item.collapseActionView();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });

    final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    //this remove left padding
    searchTextView.setDropDownAnchor(R.id.appbar);
    // this is not working
    searchTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    //and i tryed this too: searchTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.set(searchTextView, R.drawable.search_cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
    } catch (Exception e) {
    }

    return true;
}

my menu xml:

<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" tools:context=".MainActivity">
<item
    android:id="@+id/procura"
    android:title="@string/app_hint"
    android:icon="@mipmap/ic_search_black_24dp"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

And I tried these Stack Overflow answers:

solution 1

solution 2

solution 3

How can I solve this?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Bruno Ferreira
  • 1,561
  • 1
  • 10
  • 17

3 Answers3

0

Try this

searchTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT);

Instead of

searchTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

Here is my solution , By only setting the SearchView.SearchAutoComplete view DropDownWidth to match parent or width of the screen wont work because it is limited by the background resource which has padding on all sides .

So I understood that for getting it to work we must change also the background.

Solution : (this must be done each time view layout dimensions changes) :

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            SearchView.SearchAutoComplete mSearchSrcTextView = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
            mSearchSrcTextView.setDropDownWidth(getResources().getDisplayMetrics().widthPixels);
            mSearchSrcTextView.setDropDownBackgroundResource(R.color.colorGray);

            modelManager.getLocationsSearchResults(query);
            return true;
        }
});

Result

Stav Bodik
  • 2,018
  • 3
  • 18
  • 25
0

Only this worked for me:

@Override
public boolean onQueryTextChange(String query) {
    mSearchSrcTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    mSearchSrcTextView.setDropDownAnchor(((View) getParent()).getId());
    ... 
}

You must set an anchor view that has desired width. Else dropdown will not become wider than input field. I just use the parent of searchView.

As @stav-bodik mentioned: it needs to be called on every onQueryTextChange. It seems not to work if you do it only once. (background change is not needed)

You should also hold a reference to SearchAutoComplete to avoid findViewById on every text change:

SearchAutoComplete mSearchSrcTextView = seachView.findViewById(R.id.search_src_text);
allofmex
  • 557
  • 1
  • 4
  • 16