Quick Tip: Android Change Action Bar SearchView Hint (Placeholder) and Text Color

There are times when you’ll want to change the hint (placeholder) color and the text color of a SearchView widget. Here’s a small snippet that’ll allow us to do it:

[java]
int searchSrcTextId = getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchEditText = (EditText) searchView.findViewById(searchSrcTextId);
searchEditText.setTextColor(Color.BLUE); // set the text color
searchEditText.setHintTextColor(Color.BLUE); // set the hint color
[/java]

Based on the placement of the SearchView, the code to get the View object will differ. If it’s in the action bar (menu) then this is what we’ll be doing in the onCreateOptionsMenu method of the Activity or Fragment:

[java]
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
[/java]

If it is in the Activity’s (or Fragment’s) content layout then just use the findViewById() method to get the view with the given ID.

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.