How can I change a menu action item background color in a toolbar?
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:edo="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/my_action"
android:icon="@mipmap/icon"
android:title="@string/title"
app:showAsAction="always" />
</menu>
Then in my activity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_actions, menu);
final MenuItem item = menu.findItem(R.id.my_action);
MenuItemCompat.getActionView(item).setBackgroundColor(ContextCompat.getColor(this, R.color.red));
return super.onCreateOptionsMenu(menu);
}
This is not working, because getActionView always returns null.