Change the ActionBar menu icon depending on the style

I want to use different ActionBar icons depending on which style I use (dark or light). I could not understand how what I tried:

 <style name="AppThemeDark" parent="Theme.Sherlock.ForceOverflow"> <item name="android:actionButtonStyle">@style/customActionButtonStyleDark</item> <item name="actionButtonStyle">@style/customActionButtonStyleDark</item> </style> <style name="AppThemeLight" parent="Theme.Sherlock.Light.ForceOverflow"> <item name="android:actionButtonStyle">@style/customActionButtonStyleLight</item> <item name="actionButtonStyle">@style/customActionButtonStyleLight</item> </style> <style name="customActionButtonStyleDark" > <item name="@drawable/action_search">@drawable/action_search</item> </style> <style name="customActionButtonStyleLight" > <item name="@drawable/action_search">@drawable/action_search_light</item> </style> 

I also tried inserting <item name="@drawable/action_search">@drawable/action_search</item> directly into the theme style, but nothing <item name="@drawable/action_search">@drawable/action_search</item> .
Any ideas how?

+6
source share
4 answers

I solved it, but gave it a try using XML, I did it now programmatically:

 @Override public boolean onCreateOptionsMenu(Menu menu) { SharedPreferences prefs = getSharedPreferences("app", 0); boolean isDark = "Dark".equals(prefs.getString("theme", "Dark")); com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.main, menu); // set Icons menu.findItem(R.id.menuitem_search).setIcon(isDark ? R.drawable.action_search : R.drawable.action_search_light); menu.findItem(R.id.menuitem_add).setIcon(isDark ? R.drawable.content_new : R.drawable.content_new_light); menu.findItem(R.id.menuitem_share).setIcon(isDark ? R.drawable.social_share : R.drawable.social_share_light); return true; } 
+1
source

Even if you find a workaround, perhaps this will help someone else. I found an easy way to do this in xml (which logcat answer refers to). The trick I used was to create custom attributes for menu / icon icons. You must have one attribute per menu item icon.

You need to create attrs.xml in your values folder and add your custom attributes. Think of each attribute as a constant that is defined by your themes / styles, and then your styles / views can use these constants to set properties.

 <declare-styleable name="customAttrs"> <attr name="customSearchIcon" format="reference" /> </declare-styleable> 

In your styles.xml in your values folder, specify your themes / styles that set your custom icon attributes in drawable links.

 <style name="AppThemeDark" parent="Theme.Sherlock.ForceOverflow"> <item name="customSearchIcon">@drawable/action_search</item> </style> <style name="AppThemeLight" parent="Theme.Sherlock.Light.ForceOverflow"> <item name="customSearchIcon">@drawable/action_search_light</item> </style> 

Finally, in your [menu_name].xml in your menu folder, so that your menu item sets its icon to the corresponding corresponding icon attribute.

 <item android:id="@+id/menuitem_search" android:icon="?attr/customSearchIcon"/> 

Now, depending on which theme is installed, the icon for the menu item will change. In addition, it allows you to still have specific API versions of your shortcuts (light and dark) with resource identifiers with your drawable folders, so you can have different menu style icons for style icons up to 3.0 and action style icons for 3.0+.

Also remember, when you set a theme at runtime (vs AndroidManifest.xml ), you must set it before calling setContentView() in the Activity . It is recommended to restart your activity after changing the theme of an already created Activity .

+18
source

I think you did not get the theme :) When you try to do:

 <style name="customActionButtonStyleDark" > <item name="@drawable/action_search">@drawable/action_search</item> </style> 

You are trying to overload some attribute in a topic named "@ drawable / action_search"

I have bad news for you, I think there are none. So you can go to Theme.Sherlock.ForceOverflow theme and parents and see what you can overload.

If nothing helps you and you want to have a custom attribute in your theme for different icons, this is a different topic. You need to create an attribute in attrs.xml, point the source of the icon to this new attribute and determine the value of the attribute in the subject. For each button.

+2
source

See res/xml/ic_search.xml in the AppCompat blog post - age vectors (Chris Barnes)

Pay attention to the link ?attr/colorControlNormal

 <vector xmlns:android="..." android:width="24dp" android:height="24dp" android:viewportWidth="24.0" android:viewportHeight="24.0" android:tint="?attr/colorControlNormal"> <path android:pathData="..." android:fillColor="@android:color/white"/> </vector> 
0
source

Source: https://habr.com/ru/post/927104/


All Articles