1

The default SearchView has a search icon in the search hint when it is expanded. I want to remove this icon from the hint.

enter image description here

So I saw this answer on SO. It gives the following solution in onCreate():

int magId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView magImage = (ImageView) searchView.findViewById(magId);
magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));

Looking at the xml code of Android's search_view.xml here, this solution does make complete sense, but I am getting a NullPointerException at te 6th line in the following snippet:

SearchView searchView = (SearchView) findViewById(R.id.searchActivity_searchView);
        int magId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
        Log.i(TAG, "magId > "+ magId);
        ImageView magImage = (ImageView) searchView.findViewById(magId);
        Log.i(TAG, "magImage > "+ magImage);
        magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));

and before the exception, it prints:

08-07 14:13:15.740: I/SearchActivity(2548): magId > 16909188
08-07 14:13:15.740: I/SearchActivity(2548): magImage > null

So what should I do about this?

Community
  • 1
  • 1
Solace
  • 8,612
  • 22
  • 95
  • 183

1 Answers1

0

Try with this:

EditText searchEditText = (EditText) searchview.findViewById(android.support.v7.appcompat.R.id.search_src_text);
SpannableStringBuilder ssb = new SpannableStringBuilder("");
ssb.append(hintText);
searchEditText.setHint(ssb);
Joan Casadellà
  • 276
  • 2
  • 17