How to remove white underline in a SearchView widget in Toolbar Android
Asked Answered
H

11

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

Heddy answered 15/6, 2015 at 10:31 Comment(2)
try to set this two properties in xml file android:submitBackground="@android:color/transparent" android:queryBackground="@android:color/transparent"Highams
Does the thin line render automatically for you ? I am struggling to add a line !Pellmell
N
56

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.

Nakesha answered 15/6, 2015 at 10:45 Comment(5)
what do you do when the searchView has an Voice button ? i've tried to use the same trick with the search_voice_btn id but it's not worksPropagandize
Thanks a lot. In my case I set the Background drawable to null i.e. searchView.findViewById(android.support.v7.appcompat.R.id.search_plate)setBackground(null)Jamshedpur
Not help me.Underline is stay any wayAtropos
The following answer should be the accepted answer instead of this.Experiential
For AndroidX use: androidx.appcompat.R.id.search_plateForcer
L
171

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"
Legality answered 29/7, 2016 at 16:17 Comment(5)
FYI, this property is available from Api Level 21 onwards.Gordie
try app:queryBackground="@android:color/transparent" =)Latinism
app:queryBackground="@android:color/transparent" works properly on Api Level 21 instead of android:queryBackground="@android:color/transparent"Curtsey
It doesn't work for me on API 29 :( I still see the underline.Picard
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.Jemine
L
57

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

app:queryBackground="@android:color/transparent"
Lyn answered 25/5, 2018 at 10:47 Comment(1)
If 'app:' doesn't work try 'android:'. It worked for me.Spurrier
N
56

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.

Nakesha answered 15/6, 2015 at 10:45 Comment(5)
what do you do when the searchView has an Voice button ? i've tried to use the same trick with the search_voice_btn id but it's not worksPropagandize
Thanks a lot. In my case I set the Background drawable to null i.e. searchView.findViewById(android.support.v7.appcompat.R.id.search_plate)setBackground(null)Jamshedpur
Not help me.Underline is stay any wayAtropos
The following answer should be the accepted answer instead of this.Experiential
For AndroidX use: androidx.appcompat.R.id.search_plateForcer
S
43

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>
Surbase answered 14/1, 2016 at 18:15 Comment(3)
This is the best answer as it doesn't make assumptions about the view hierarchy implementation of the support library, which may change without notice.Stanley
Indeed a best answer! Prefer this first before trying any other solution.Dorren
Nope. Does not workCreolacreole
A
12

For AndroidX I used this based on other answers here,

searchView.findViewById(androidx.appcompat.R.id.search_plate)
        .setBackgroundColor(Color.TRANSPARENT)
Asleep answered 25/3, 2019 at 13:29 Comment(1)
Awesome solution Thanks:)Feat
E
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"
                />
Elfont answered 10/2, 2022 at 14:54 Comment(0)
H
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));
Haematoxylon answered 21/4, 2017 at 11:16 Comment(1)
Instead of ContextCompat.getColor... simply use Color.TRANSPARENT.Antirachitic
P
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
Piacular answered 2/11, 2019 at 14:27 Comment(0)
S
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)
Sagittal answered 27/7, 2020 at 15:53 Comment(0)
C
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.

Chlorella answered 7/11, 2016 at 7:0 Comment(1)
Actually, this is the only answer that worked for me. An expanded explanation on why this works is available hereAlanealanine
D
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!

Deuno answered 9/8, 2019 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.