In my application, I implement the ability for a user to choose a theme. I do this through PreferenceActivity. The problem is that the toolbara icons do not respond to switching themes. toolbarDark enter image description here

My code is:

<item android:id="@+id/settings" android:icon="@drawable/dots_vertical" android:orderInCategory="100" android:title="@string/settings" app:showAsAction="ifRoom"/> 

 <resources> <style name="AppThemeLight" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/primaryLight</item> <item name="colorPrimaryDark">@color/primaryDarkLight</item> <item name="colorAccent">@color/accentLight</item> <item name="actionOverflowButtonStyle">@style/ActionButtonOverflow</item> </style> <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primaryDark</item> <item name="colorAccent">@color/accent</item> <item name="actionOverflowButtonStyle">@style/ActionButtonOverflowLight</item> </style> <style name="ActionButtonOverflowLight" parent="android:style/Widget.Holo.Light.ActionButton.Overflow"> <item name="android:src">@drawable/ic_dots_vertical_white_24dp</item> </style> <style name="ActionButtonOverflow" parent="android:style/Widget.Holo.ActionButton.Overflow"> <item name="android:src">@drawable/ic_dots_vertical_black_24dp</item> </style> 

There are a lot of different tips on SO, but there either use another parent (but I have a custom toolbar, so I can’t use, say, Theme.AppCompat.Dark.ActionBar) or workarounds, like in my code above - create a custom theme and register icon there. (that didn't work either)

Also the fact is that in my other application the theme changed when the menu item was selected. I had a packet with different sizes and colors in my resources.

enter image description here

and in the menu I simply wrote android: icon = "@ drawable / dots_vertical" (without size and color), everything worked. And in ways to work around my real problem, you need to write @ drawable / ic_dots_vertical_black_24dp. But the application will be used on different screens and if I correctly understand the system itself will pull an acceptable resource.

Therefore, I want to do not only work, but correctly. Specialists, help figure out

  • Maybe the problem is that you set the style of your toolbar in the xml markup? - McDaggen
  • No, I do not ask there. - Zzzzz Zzzzz

1 answer 1

To do what you want, you must first define your custom attribute in the values ​​/ attr.xml file :

 <attr name="menu_icon" format="reference" /> 

This attribute will be responsible for our icon. The value of this attribute must be written in your themes, where it will accept sprawling icons depending on the topics:

 <style name="AppThemeLight" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/primaryLight</item> <item name="colorPrimaryDark">@color/primaryDarkLight</item> <item name="colorAccent">@color/accentLight</item> <item name="menu_icon">@drawable/ic_dots_vertical_white_24dp</item> </style> <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primaryDark</item> <item name="colorAccent">@color/accent</item> <item name="menu_icon">@drawable/ic_dots_vertical_black_24dp</item> </style> 

After that you specify it in the menu, in value android: icon = "? Menu_icon" , where ? means that we take the value from the custom attribute - the value is taken from the topic:

 <item android:id="@+id/settings" android:icon="?menu_icon" android:orderInCategory="100" android:title="@string/settings" app:showAsAction="ifRoom"/>