2

I have a weird problem at my hands. I need to be able to vertically center the hint text of a SearchView widget so that it aligns with the magnifying glass and the close icon.

This is what the SearchView currently looks like. Notice how the text view is vertically off centered in comparison to the magnifying glass and the x icon

Here's a snippet of what I have so far:

SearchView sv = (SearchView) view.findViewById(R.id.search);

int id = sv.getContext().getResources().getIdentifier("android:id/search_src_text", null, null); 
int searchPlate = sv.getContext().getResources().getIdentifier("android:id/search_plate", null, null); 
int searchMagIcon = sv.getContext().getResources().getIdentifier("android:id/search_mag_icon", null, null); 
ImageView magIcon = (ImageView) sv.findViewById(searchMagIcon); View searchPlateView = sv.findViewById(searchPlate); 
EditText searchText = (EditText) sv.findViewById(id); searchText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);

//searchText.setHint("Some test hint text here"); 
searchPlateView.setBackgroundColor(Color.WHITE); 
searchText.setHintTextColor(Color.parseColor("#D8D8D8")); 
sv.setIconified(false); 
sv.clearFocus();

In the commented out line, I've found that this aligns perfectly with the close icon to the right, however, this gets rid of the magnifying glass icon. I've tried using reflection, outlined in this article http://nlopez.io/how-to-style-the-actionbar-searchview-programmatically/

However, this moved both the hint text and the magnifying glass and did some other weird stuff. From my observations, it seems that the magnifying glass is actually tied to the hint text, although in the above code (which I've scoured from the android code base) it seems that the magnifying glass is its own thing? I have tried to play around with it, such as creating an ImageView from the magnifying glass id. But nothing came out of it.

David
  • 53
  • 1
  • 6

1 Answers1

0

I feel I should answer my own question after a couple month break off this project. I was asking the wrong question and should have realized that after the comment above about "The magnifying glass is an ImageSpan, so yes, it is tied to the hint text."

The real answer to this is, align the text around the imagespan. And there's an answer to that! The link is here: Align text around ImageSpan center vertical

Community
  • 1
  • 1
David
  • 53
  • 1
  • 6