How to delete or change the searchview icon inside the SearchView actionBar?

后端 未结 8 2147
臣服心动
臣服心动 2020-12-17 04:57

\"screenshot\"

How to remove or change the search view icon inside the edittext? I am using the Appcompat libra

相关标签:
8条回答
  • 2020-12-17 05:27

    remove is the right drawable that you have mentioned in xml:

    final Drawable remove = ContextCompat.getDrawable(getActivity(), R.drawable.ic_close_circle);
            etSearchProducts.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    if (etSearchProducts.getCompoundDrawables()[2] == null) {
                        return false;
                    }
                    if (event.getAction() != MotionEvent.ACTION_UP) {
                        return false;
                    }
                    if (event.getX() > etSearchProducts.getWidth() - etSearchProducts.getPaddingRight() - remove.getIntrinsicWidth()) {
                        etSearchProducts.setText("");
                        Utility.hideKeyboard(getActivity());
                    }
    
                    return false;
                }
            });
    
    0 讨论(0)
  • 2020-12-17 05:28

    I used the menu item with the property app:actionViewClass="android.support.v7.widget.SearchView"

    On the activity I set:

    searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setIconifiedByDefault(false);
    

    To remove the icon I did:

    ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
    searchIcon.setImageDrawable(null);
    
    0 讨论(0)
  • 2020-12-17 05:28

    no a good idea use private method as SearchView.class.getDeclaredField("mSearchHintIcon");

    <item android:id="@+id/menu_search"
            android:icon="@drawable/action_search"
            app:actionLayout="@layout/xx_layout"
            app:showAsAction="always"/>
    

    and in xx_layout:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/search_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:searchHintIcon="@null"
        android:iconifiedByDefault="true" />
    
    0 讨论(0)
  • 2020-12-17 05:32

    Finally found the solution. Try the following:

    try {
        Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
        mDrawable.setAccessible(true);
        Drawable drawable =  (Drawable)mDrawable.get(your search view here);
        drawable.setAlpha(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-12-17 05:33

    Simplest way is

    1. In case you are using AppCompat use

          app:iconifiedByDefault="false"
          app:searchIcon="@null"
      
    2. Else use android in place of app in above code as

      android:iconifiedByDefault="false"
      android:searchIcon="@null"
      
    0 讨论(0)
  • 2020-12-17 05:40

    I was digging about that a lot and finally I found this solution and it works for me!
    You can use this

    If you use appcompat try this:

    ImageView searchIconView = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
    searchIconView.setImageResource(R.drawable.yourIcon);
    

    If you use androidx try this:

    ImageView searchIconView = (ImageView) searchView.findViewById(androidx.appcompat.R.id.search_button);
        searchIconView.setImageResource(R.drawable.yourIcon);
    

    It will change the default serchview icon.

    Hope it helps!

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