Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling a SearchView in Android Action Bar

I have a search widget in my Action Bar like this:

Search Widget in Action Bar

(1) How do I change the color of the text that says "iPhone"?

(2) Also, if you notice the gray X -- the entire Search Widget is that color as well when it is in an icon position. I am on Holo.Theme.Light and utilizing my own mods to it.

How do I change these two styles for the widget in my styles.xml file (assuming that is where you make the changes for a search widget)?

like image 505
TheLettuceMaster Avatar asked Jul 17 '12 17:07

TheLettuceMaster


People also ask

How can I customize my action bar in Android?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I replace my action bar toolbar?

To replace an app's default action bar with a Toolbar : Create a new custom theme and modify the app's properties so that it uses this new theme. Disable the windowActionBar attribute in the custom theme and enable the windowNoTitle attribute. Define a layout for the Toolbar .


2 Answers

I have been spending many time for this but finally: :-)

  1. To change the text color :
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE); 

or this one for AndroidX:

((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE); 
  1. To change the text hint:
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE); 

or this one for AndroidX:

((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE); 
like image 179
Adidas Avatar answered Sep 21 '22 17:09

Adidas


If you're using appcompat library, then the solution is a bit different from Jerome's answer. Here's my solution

@Override public boolean onCreateOptionsMenu(Menu menu) {     getMenuInflater().inflate(R.menu.main_menu, menu);     restoreActionBar();      SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);     MenuItem searchMenuItem = menu.findItem(R.id.action_search);     SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));      SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);     searchAutoComplete.setHintTextColor(Color.WHITE);     searchAutoComplete.setTextColor(Color.WHITE);      View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);     searchplate.setBackgroundResource(R.drawable.texfield_searchview_holo_light);      ImageView searchCloseIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);     searchCloseIcon.setImageResource(R.drawable.clear_search);      ImageView voiceIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn);     voiceIcon.setImageResource(R.drawable.abc_ic_voice_search);      ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);     searchIcon.setImageResource(R.drawable.abc_ic_search);       return super.onCreateOptionsMenu(menu); } 
like image 37
Abdul Rahman Avatar answered Sep 21 '22 17:09

Abdul Rahman