How to change searchview icon color?

后端 未结 9 1212
北海茫月
北海茫月 2020-12-19 13:19

I know there\'s already a lot of answers for this question here, and I wouldn\'t be creating another thread if I hadn\'t already tried everything I saw here. Anyway, the qu

相关标签:
9条回答
  • 2020-12-19 13:52

    If you want to change SearchView's search icon, you just need to get the image view within the view as follow:

    searchView = v.findViewById(R.id.searchView);
    //change icon color
    ImageView searchIcon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
    searchIcon.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_search_icon));
    

    It is important that the icon is R.id.search_button and you can replace it with a white vector asset that you provide, in this case R.drawable.ic_search_icon

    Similarly, you can change the text within the SearchView as follows:

    //set color to searchview
    SearchView.SearchAutoComplete searchAutoComplete = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchAutoComplete.setHintTextColor(getResources().getColorandroid.R.color.white));
    searchAutoComplete.setTextColor(getResources().getColor(android.R.color.white));
    
    0 讨论(0)
  • 2020-12-19 13:54

    Use below code:

    ImageView searchIcon = searchView.findViewById(R.id.search_button);
    searchIcon.setColorFilter(Color.WHITE);
    

    If you want to change close icon color, use this one:

    ImageView searchClose = searchView.findViewById(R.id.search_close_btn);
    searchIcon.setColorFilter(Color.WHITE);
    
    0 讨论(0)
  • 2020-12-19 13:59

    For android.x,

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        ...
    
        ImageView searchIcon= 
        searchView.findViewById(androidx.appcompat.R.id.search_mag_icon);
    
        // To change color of close button, use:
        // ImageView searchCloseIcon = (ImageView)searchView
        //        .findViewById(androidx.appcompat.R.id.search_close_btn);
    
        searchIcon.setColorFilter(getResources().getColor(R.color.colorPrimary),
                android.graphics.PorterDuff.Mode.SRC_IN);
    
        ...
    
        return true;
    
    }
    
    0 讨论(0)
  • 2020-12-19 14:04

    To change the search view's icon color use the following lines of code:

    SearchView searchView = (SearchView) findViewById(R.id.searchview);
    ImageView icon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
    icon.setColorFilter(Color.BLACK);
    
    0 讨论(0)
  • 2020-12-19 14:04

    You can change or customize the icons of the SearchView by adding you're own drawable icons.

            <androidx.appcompat.widget.SearchView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            app:closeIcon="@drawable/close_icon"
            app:searchHintIcon="@drawable/search_icon"
            app:searchIcon="@drawable/search_icon"
            android:textStyle="bold" />
    
    0 讨论(0)
  • 2020-12-19 14:06

    Take the Drawable you want to tint, wrap it with a tinted drawable , and set it as a new one for the search menu item:

    @JvmStatic
    fun getTintedDrawable(inputDrawable: Drawable, @ColorInt color: Int): Drawable {
        val wrapDrawable = DrawableCompat.wrap(inputDrawable.mutate())
        DrawableCompat.setTint(wrapDrawable, color)
        DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN)
        return wrapDrawable
    }
    

    Usage:

    val drawable =getTintedDrawable(...)
    searchMenuItem.icon = drawable
    

    And this should work for all Android versions that android-x (support library) supports.

    0 讨论(0)
提交回复
热议问题