92

I am using the Toolbar search widget in my project. Everything works fine but expect the thing which I am completely stuck up with removing the underline below the search field in my toolbar. I have tried many solutions and nothing works out. Below are some of the solutions that I tried.

Requirement is to remove white underline in the image

enter image description here

Ist Solution:

//Removing underline

    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
    View searchPlate = searchView.findViewById(searchPlateId);
    if (searchPlate!=null) {
        searchPlate.setBackgroundColor (Color.TRANSPARENT);
        int searchTextId = searchPlate.getContext ().getResources ().getIdentifier ("android:id/search_src_text", null, null);

    }

IInd Solution:

 EditText searchEdit = ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text));
 searchEdit.setBackgroundColor(Color.TRANSPARENT);

The above code works to change the background of EditText but still underline is displaying below the close icon in SearchBar.

Complete code that I am using for SearchBar widget as follows:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater infl) {
        super.onCreateOptionsMenu(menu, infl);
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.action_search, menu);
        final SearchView searchView = (SearchView) MenuItemCompat
                .getActionView(menu.findItem(R.id.search));

        SearchManager searchManager = (SearchManager) getActivity ().getSystemService (getActivity ().SEARCH_SERVICE);
        searchView.setSearchableInfo (searchManager.getSearchableInfo (getActivity ().getComponentName ()));

        //changing edittext color
        EditText searchEdit = ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text));
        searchEdit.setTextColor(Color.WHITE);
}

action_search.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:compat="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search"
        android:title="Search"
        android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
        compat:showAsAction="ifRoom|collapseActionView"
        compat:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

possible duplicate of this

Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33
Chandru
  • 5,954
  • 11
  • 45
  • 85
  • 1
    try to set this two properties in xml file android:submitBackground="@android:color/transparent" android:queryBackground="@android:color/transparent" – Abhishek May 17 '16 at 03:06
  • Does the thin line render automatically for you ? I am struggling to add a line ! – user1841702 Aug 01 '16 at 15:51

11 Answers11

168

If you are using SearchView then use for API below 21

android:queryBackground="@android:color/transparent"

you can use below code if you are using API 21 or more

app:queryBackground="@android:color/transparent"
Kshitij Jain
  • 2,103
  • 2
  • 13
  • 18
  • 1
    FYI, this property is available from Api Level 21 onwards. – Chintan Soni Oct 21 '16 at 06:12
  • 4
    try app:queryBackground="@android:color/transparent" =) – Vlad Zbuker Mar 15 '17 at 10:17
  • 1
    app:queryBackground="@android:color/transparent" works properly on Api Level 21 instead of android:queryBackground="@android:color/transparent" – Nikhil Apr 13 '17 at 07:44
  • 2
    It doesn't work for me on API 29 :( I still see the underline. – Hesam Jul 29 '20 at 21:34
  • 2
    TBH, it is **NOT API LEVEL** that makes decision, it is **the VIEW type you use**. For example, I use `SearchView` (not `androidx.appcompat.widget.SearchView` ) in API 28, then I should use `android:` prefix instead. – Samuel T. Chou May 18 '21 at 04:39
57

Once try as follows

View v = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
v.setBackgroundColor(Color.parseColor("here give actionbar color code"));

Hope this will helps you.

Nagaraju V
  • 2,739
  • 1
  • 14
  • 19
57

Just add this line if you are using a v7 or androidx widget

app:queryBackground="@android:color/transparent"
AyTee
  • 489
  • 1
  • 8
  • 19
42

Or you can do this through styles:

<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
    <item name="submitBackground">@color/primaryColor</item>
    <item name="queryBackground">@color/primaryColor</item>
</style>

queryBackground is attribute for the query background. If you support voice search you may want to remove the line also under that mic button. Thats what submitBackground attribute is for.

Then of course apply the style to your theme.

<style name="Theme.App" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
    <item name="searchViewStyle">@style/SearchViewMy</item>
</style>
Tomask
  • 2,344
  • 3
  • 27
  • 37
12

For AndroidX I used this based on other answers here,

searchView.findViewById(androidx.appcompat.R.id.search_plate)
        .setBackgroundColor(Color.TRANSPARENT)
Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81
7

if you want to remove the underline from SearchView, then just copy paste the below code to your SearchView. it works

 android:queryBackground="@null"

like this,

      <SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:queryBackground="@null"
                />
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
6

You can try using the below code, works like a charm for me.

View v = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    v.setBackgroundColor(ContextCompat.getColor(context,android.R.color.transparent));
ranasaha
  • 165
  • 3
  • 10
3

The below piece of code works for me.

val searchView = menu?.findItem(R.id.action_search)?.actionView as SearchView
searchView.findViewById<View>(androidx.appcompat.R.id.search_plate).background = null
Ganesa Vijayakumar
  • 2,422
  • 5
  • 28
  • 41
2

For androidx and kotlin:

        menuInflater.inflate(R.menu.conversation_list_screen_menu, menu)
        val searchItem = menu.findItem(R.id.action_search)
        searchView = searchItem.actionView as SearchView
        searchView?.findViewById<View>(androidx.appcompat.R.id.search_plate)?.setBackgroundColor(Color.TRANSPARENT)
Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43
0

The code for changing the background color of SearchView edit text is below

public static void setSearchViewEditTextBackgroundColor(Context context, SearchView searchView, int backgroundColor){
    int searchPlateId = context.getResources().getIdentifier("android:id/search_plate", null, null);
    ViewGroup viewGroup = (ViewGroup) searchView.findViewById(searchPlateId);
    viewGroup.setBackgroundColor(ComponentUtils.getColor(context, backgroundColor));
}

If your action bar background color is white then call the above method as follow.

setSearchViewEditTextBackgroundColor(this, searchView, R.color.white);

Now the underline from SearchView edit text will disappear.

Siddarth Kanted
  • 5,738
  • 1
  • 29
  • 20
  • Actually, this is the only answer that worked for me. An expanded explanation on why this works is available [here](https://stackoverflow.com/questions/11085308/changing-the-background-drawable-of-the-searchview-widget/11669808#11669808) – Igor Jun 05 '18 at 22:47
0

First you should get the bottom line simply like this:

searchPlate = (View) findViewById(R.id.search_plate);

Then you should set background color to transparent like this:

searchPlate.setBackgroundColor(Color.TRANSPARENT);

This works perfectly for androidx.appcompat.widget.SearchView!

Ali Najafi
  • 53
  • 6