Search code examples
androidandroid-layoutsearchview

SearchView in fragment (non in toolbar) can't remove underline


In my fragment I use searchView (not in toolbar). My layout snippet:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.SearchView
        android:id="@+id/searchView"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_marginEnd="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        app:queryHint="Some hint"
        android:layout_marginStart="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:searchHintIcon="@null" />
</android.support.constraint.ConstraintLayout>

In my fragment's code :

        searchView = view.findViewById(R.id.searchView);
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setSuggestionsAdapter(new SearchCatalogSuggestionsAdapter(getActivity()));
searchView.setBackgroundColor(Color.parseColor("#FFFFFF"));

As you can see I try

searchView.setBackgroundColor(Color.parseColor("#FFFFFF"));

But underline is not help. Here screenshot:

Search-view-underline


Solution

  • AppCompat supplies two styles for the SearchView:

    • the default with underline
    • one for use in the Action Bar without underline

    You can change the style like this:

    <android.support.v7.widget.SearchView
        style="@style/Widget.AppCompat.SearchView.ActionBar"/>
    

    The style looks like this:

    <style name="Widget.AppCompat.SearchView.ActionBar" parent="Base.Widget.AppCompat.SearchView.ActionBar"/>
    
    <style name="Base.Widget.AppCompat.SearchView.ActionBar">
        <item name="queryBackground">@null</item>
        <item name="submitBackground">@null</item>
        <item name="searchHintIcon">@null</item>
        <item name="defaultQueryHint">@string/abc_search_hint</item>
    </style>
    

    You could use the attributes directly, of course.