Search code examples
androidmenuitemandroidxsearchview

Update androidx SearchView text color in toolbar


I am having a ton of issues trying to figure out how to change the text on my androidx searchview either via programmatically or within the xml theme. Below is my relevant code. How can I update the text from black to white so it shows on a black background like the screenshot?

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search"
        android:title="Search"
        android:icon="@drawable/search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="com.exposure.controls.ExposureSearchView" />
</menu>

public class ExposureSearchView extends SearchView {

    public ExposureSearchView(Context context) {
        super(context);
        //((ImageView) findViewById(androidx.appcompat.R.id.search_button)).setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.search));
    }
    
    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    private boolean disabled;
    private MenuItem menuItem;
    private boolean expanded;

    public void storeMenuItem(MenuItem menuItem) {
        this.menuItem = menuItem;
    }
    
    public MenuItem getMenuItem() {
        return this.menuItem;
    }
    
    @Override
    public void onActionViewExpanded() {
        super.onActionViewExpanded();
        expanded = true;
    }

    @Override
    public void onActionViewCollapsed() {
        super.onActionViewCollapsed();
        expanded = false;
    }

    public boolean isExpanded() {
        return expanded;
    }
}

enter image description here


Solution

  • If you use a fragment or Activity you can add the code below on your onCreateOptionsMenu ()

    EditText txtSearch = (searchView.findViewById(androidx.appcompat.R.id.search_src_text));
    txtSearch.setTextColor(Color.WHITE);