Skip to content

Instantly share code, notes, and snippets.

Last active December 11, 2018 21:29
Show Gist options
  • Save dbachelder/9587898 to your computer and use it in GitHub Desktop.
Save dbachelder/9587898 to your computer and use it in GitHub Desktop.
style the actionbar compat searchview. inspired by this article:
final SupportMenuItem searchMenuItem = (SupportMenuItem) menu.findItem(;
if (searchMenuItem == null) throw new IllegalArgumentException("menu item is null and that is very suspicious.");
this.searchView = (SearchView) searchMenuItem.getActionView();
if (searchView == null) throw new IllegalArgumentException("search view is null and that is very suspicious.");
// wow. much hack. style search box..
Resources resources = searchView.getContext().getResources();
TextView searchBox = (TextView) searchView.findViewById(;
ImageView closeBtn = (ImageView) searchView.findViewById(;
// create new hint span
SpannableStringBuilder stopHint = new SpannableStringBuilder(" ");
// Add the icon as an spannable
Drawable searchIcon = FontIconDrawable.inflate(resources, R.xml.ic_ab_search);
closeBtn.setImageDrawable(FontIconDrawable.inflate(resources, R.xml.ic_ab_close));
Float rawTextSize = searchBox.getTextSize();
int textSize = (int) (rawTextSize * 1.25);
searchIcon.setBounds(0, 0, textSize, textSize);
stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the new hint text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment