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

后端 未结 10 2014
挽巷
挽巷 2020-12-01 03:33

I am using Toolbar search widget in my project. Everything works fine but expect the thing which I am completely stuck up with removing the underline below search field in m

相关标签:
10条回答
  • 2020-12-01 03:43

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

    app:queryBackground="@android:color/transparent"
    
    0 讨论(0)
  • 2020-12-01 03:43

    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!

    0 讨论(0)
  • 2020-12-01 03:45

    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));
    
    0 讨论(0)
  • 2020-12-01 03:50

    For AndroidX I used this based on other answers here,

    mSearchView.findViewById(androidx.appcompat.R.id.search_plate)
            .setBackgroundColor(Color.TRANSPARENT);
    
    0 讨论(0)
  • 2020-12-01 03:52

    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.

    0 讨论(0)
  • 2020-12-01 03:53

    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
    
    0 讨论(0)
提交回复
热议问题