How to change toolbar style programmatically? When I try to do this popup menu changes instead of the toolbar

Viewed 27

I have a custom toolbar for my app, but in one of the activities I want to change the style of the toolbar programatically. This is the style for the toolbar that I want to apply programmatically.

<style name="toolbarStyle" parent="ThemeOverlay.AppCompat.Dark">
    <item name="android:background">@color/teal_200</item>
    <item name="colorAccent">@android:color/white</item>
</style>

I use this code to change toolbar style programmatically

toolbar.getContext().setTheme(R.style.toolbarStyle);

The problem is, the popup theme changes instead of the toolbar.

How to fix it?

1 Answers

Are you in activity or fragment? If activity, try getSupportActionBar() to get the toolbar used

Related