Search code examples
javaandroidlistviewsearchview

No divider in suggestion list of SearchView


I followed the example of Google about custom suggestion. My project is very simple:

  1. One ContentProvider.class
  2. One SqlOpenHelper.class
  3. One MainActivity.class
  4. One menu and one layout, no other XML layout/drawable is created.

I have a SearchView (support.v7) in the Toolbar of AppCompatActivity (AppCompat.v7) and tested with Android 5.1.1 and Android 4.3.

The SearchView in the menu is simple:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="always"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:title="Search"/>
  </menu>

It is able to get the suggestion from a simple CustomContentProvider. However, there is no divider in the suggestion list (see the image). Before I also created a similar search function in a normal layout of another app. It worked normally but this time the SearchView is in the Menu/Toolbar.

enter image description here

I was able to change the background of the suggestion list with this code:

private void initSearchView(MenuItem searchItem) {
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
    SearchView searchView = (SearchView) searchItem.getActionView();

    SearchView.SearchAutoComplete autoCompleteTextView = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);

    /**
     * Todo: Change row style of suggestion List
     */
    if (autoCompleteTextView != null) { 
    autoCompleteTextView.setDropDownBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.search_suggestion_row));
    }
    searchView.setSearchableInfo(searchableInfo);
}

I have tried by changing the theme of Activity with no luck and I don't think I should create a row layout and a ListAdapter again.

Any solution for changing the style of the row or show the divider. Thanks for any help!


Solution

  • The divider is not appearing because it a bug in lollipop, if you override the **isEnabled()** method this happens, here refer this and you can know more. https://code.google.com/p/android/issues/detail?id=83055