Android SearchView Icon

24,134

Solution 1

In your theme:

<style name="Theme" parent="Your parent theme">
<item name="android:searchViewSearchIcon">@android:drawable/ic_search</item>
</style>

Edit:
searchViewSearchIcon is a private attribute. This answer therefore does not work (on the native ActionBar).

Solution 2

Since the wording of the question does not contain any mention of ActionBarSherlock (except for the tag) and comments have been added that the accepted answer does not work for standard and/or support library action bar, I'm posting another answer. Here is the Java code for onCreate:

// obtain action bar
ActionBar actionBar = getSupportActionBar();

// find SearchView (im my case it's in a custom layout because of left alignment)
View v = actionBar.getCustomView();
SearchView searchView = (SearchView)v.findViewById(R.id.search_view);
ImageView icon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);

// method 1: does not work persistently, because the next line
// should be probably called after every manipulation with SearchView
// icon.setVisibility(View.GONE);

// method 2: working code
icon.setAdjustViewBounds(true);
icon.setMaxWidth(0);
icon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
icon.setImageDrawable(null);

According to this post, while using a SearchView from the support library (android-support-v7-appcompat in my case) with setIconifiedByDefault(false) the icon is displayed outside edit textbox (SearchAutoComplete). Otherwise (setIconifiedByDefault(true)) it's displayed inside the box. The presented code is used for disabled "iconification" by default, and may require some changes to work with "iconified" search view. I don't know if the same applies to ActionBarSherlock.

Hope this helps.

Solution 3

This icon is hint. So you can simply set new hint text.

int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);

    searchPlate.setHint("Search");

If you want icon as hint text use spanable string with ImageSpan

int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);


    searchPlate.setTextColor(getResources().getColor(R.color.search_text_color));
    SpannableString string = new SpannableString(" ");
    Drawable d = getResources().getDrawable(R.drawable.ic_search);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
    string.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    searchPlate.setHint(string);

Solution 4

You have to add this line in your MenuItem android:iconifiedByDefault="@android:color/transparent"

<item
    android:id="@+id/activity_home_action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/activity_home_search_title"
    android:iconifiedByDefault="@android:color/transparent"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView" />

Or you can make programatically searchView.setIconifiedByDefault(true);

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    getMenuInflater().inflate(R.menu.home, menu);

    MenuItem searchMenuItem = menu.findItem(R.id.activity_home_action_search);

    SearchView searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setIconifiedByDefault(true);
}

Solution 5

To remove the hint search button:

mNearMeSearchBar = (SearchView) mView.findViewById(R.id.nearme_search_component_nearme_edittext);
mNearMeSearchBar.setIconifiedByDefault(false); //Removes Search Hint Icon

To change the search button drawable:

int searchImgId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView v = (ImageView) mNearMeSearchBar.findViewById(searchImgId);
v.setImageResource(R.drawable.ic_compass);
Share:
24,134
Harsha M V
Author by

Harsha M V

I turn ideas into companies. Specifically, I like to solve big problems that can positively impact millions of people through software. I am currently focusing all of my time on my company, Skreem, where we are disrupting the ways marketers can leverage micro-influencers to tell the Brand’s stories to their audience. People do not buy goods and services. They buy relations, stories, and magic. Introducing technology with the power of human voice to maximize your brand communication. Follow me on Twitter: @harshamv You can contact me at -- harsha [at] skreem [dot] io

Updated on October 31, 2020

Comments

  • Harsha M V
    Harsha M V over 3 years

    I want to disable the Mag icon displayed inside the search view component. Any idea how to reference it and remove it or replace it with another drawable ?

    enter image description here