Solution 1 :
You can’t change it from OptionsMenu
. Instead, you can try programmatically using below code:
Kotlin with AndroidX:
val searchView: SearchView = item.getActionView();
val editText = searchView.findViewById<EditText>(androidx.appcompat.R.id.search_src_text)
editText.setTextColor(getResources().getColor(R.color.white));
editText.setHintTextColor(getResources().getColor(R.color.white));
Java with AndroidX:
SearchView searchView = (SearchView) item.getActionView();
EditText editText = (EditText) searchView.findViewById(androidx.appcompat.R.id.search_src_text);
editText.setTextColor(getResources().getColor(R.color.white));
editText.setHintTextColor(getResources().getColor(R.color.white));
Solution 2 :
In the parent theme add the following:
- In the parent theme add the following:
<item name="android:editTextColor">@android:color/darker_gray</item>
Note that you can replace “@android:color/darker_gray” with your desired color.
For changing the hint you can use this:
android:queryHint="my_hint_here"
follow this link for more details
Solution 3 :
Below code worked for me:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu,inflater);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) item.getActionView();
EditText editText = (EditText) searchView.findViewById(androidx.appcompat.R.id.search_src_text);
editText.setTextColor(Color.WHITE);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
item.setActionView(searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
searchView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}
);
}
Solution 4 :
Add a custom theme to your Toolbar.
Something like this
<androidx.appcompat.widget.Toolbar
android_id="@+id/toolbarMenu"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_theme="?ref_toolbar_icon_tint"
app_popupTheme="?ref_toolbar_popup_menu">
Then in your styles.xml add the following
<style name="toolbar_icons_theme_dark" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">--color of the icons--</item>
<item name="android:editTextColor">--text color--</item>
<item name="android:textColorHint">--hint color--</item>
</style>
and just call this custom style into your parent theme
<item name="ref_toolbar_icon_tint">@style/toolbar_icons_theme_dark</item>
Problem :
I implemented a search view in options menu by using the following menu code.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto">
<item
android_id="@+id/search_toolBar"
android_title="Search"
android_icon="@drawable/search_icon"
app_showAsAction="always|collapseActionView"
app_actionViewClass="androidx.appcompat.widget.SearchView"
/>
</menu>
How to change the color of text of my search view. I want to also change the hint of searchview.