Skip to content

Instantly share code, notes, and snippets.

@mauriciogior
Created February 24, 2015 22:01
Show Gist options
  • Save mauriciogior/7744521aaba3b15fc254 to your computer and use it in GitHub Desktop.
Save mauriciogior/7744521aaba3b15fc254 to your computer and use it in GitHub Desktop.
Change cursor in search view widget for AppCompat v21
// We need to use reflection...
// yes, I know, it sucks!
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_with_search, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
final EditText editTextView = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(editTextView, 0); // 0: @null (automatic), or any drawable of yours.
} catch (Exception e) {
e.printStackTrace();
}
}
@RTimal
Copy link

RTimal commented Sep 5, 2015

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment