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:

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

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:

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*