Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

How to remove white underline in a SearchView widget in Toolbar Android

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...