How to tint menu icons is already covered a few times, like here:
Toolbar icon tinting on Android
Additionally to this solution there is still the problem of the navigation icon.
Applying a Theme(Overlay) to your Toolbar just tints the text and the whitelisted icons (see: https://stackoverflow.com/a/26817918/2417724)
If you set a custom icon (which happens to be quite easy the case, as you need to change it if you don't want to display the default back arrow) then this custom icon does not get tinted.
How do you handle your Icons then?
All my icons are per default black and I don't want to have special white versions just to use them in the Toolbar then.
The appcompat navigation button - which is simply an AppCompatImageButton - can be styled through the toolbarNavigationButtonStyle attribute. The default style for that in the AppCompat themes is Widget.AppCompat.Toolbar.Button.Navigation, and we can extend that style to add a tint attribute value. For example:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="toolbarNavigationButtonStyle">#style/Toolbar.Button.Navigation.Tinted</item>
</style>
<style name="Toolbar.Button.Navigation.Tinted" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="tint">#color/nav_button_tint</item>
</style>
There are a couple of caveats to be aware of when using this method.
Prior to support library version 25.4.0, AppCompatImageButton did not offer its own tint attribute, and therefore a tint attribute in the app's namespace will not apply (and just won't exist, unless defined elsewhere). It is necessary to use the platform android:tint attribute if using support library version 25.3.0 or earlier.
Unfortunately, this leads to another catch, in that the platform tint prior to Lollipop (API level 21) can handle only simple, single color values, and using a ColorStateList (<selector>) resource value will cause an Exception to be thrown. This poses no problems if the android:tint value is a simple color, but it is often desired to tint the navigation icon to match another theme color attribute, which may very well be a ColorStateList. In this case, it would be necessary to create separate styles in res/values/ and res/values-21/, specifying a simple color value for android:tint in res/values/.
For example, if tinting to match the theme's primary text color:
res/values/styles.xml
<item name="android:tint">#color/normal_text_color</item>
res/values-v21/styles.xml
<item name="android:tint">?android:textColorPrimary</item>
You need only concern yourself with the notes above if you're stuck using a support library version less than 25.4.0.
To effectively set the tint color of the navigation icon programmatically you need to set the drawable first and apply the tint afterwards.
toolbar.setNavigationIcon(R.drawable.ic_back)
toolbar.children.forEach {
(it as? AppCompatImageButton)?.imageTintList =
ColorStateList.valueOf(Color.GREEN)
it.refreshDrawableState()
}
Related
I am using the Leanback library for an Android TV application, and I am using the typical BrowseFragment that uses ImageCardViews for navigation. The ImageCardViews show a ripple effect when clicked: a semi-opaque white circle starts in the middle and grows to fill the View.
I am trying to change the ripple color to match my app's primary color. My application uses a custom Theme that inherits from Theme.Leanback, and I thought (based on e.g. https://stackoverflow.com/a/31922339/925478) that I could change the ripple color by overriding the colorControlHighlight attribute in the theme:
<style name="MyLeanback" parent="Theme.Leanback">
<item name="colorControlHighlight">#color/red</item>
</style>
However, this does not seem to have any effect. When I click and hold an ImageCardView, I still see the semi-opaque white circle.
How can I change the color of the ripple?
The solution was maddeningly easy:
<style name="MyLeanback" parent="Theme.Leanback">
<item name="android:colorControlHighlight">#color/red</item>
</style>
Note the android: prefix, which is required to override themes when not using AppCompat.
I had been dissuaded by the editor from including that prefix; it complained that it requires API21 (my application targets a lower version because it is a single .APK for mobile and TV platforms).
Can I change the colorPrimaryDark color of material theme programmatically?
I want to apply different colorPrimaryDark color based on the navigation Item choosed, is there any alternative other than setting different styles?
There is no way such as per current available methods of android sdk. So Instead of changing colorPrimaryDark attribute programatically, I am applying different themes for different activities
I know it's possible to replace the icon by setting a custom actionOverflowButtonStyle as described here. However, that style only allows setting a different drawable and or/background.
Instead, I would like to use the standard icon provided by AppCompat, just tinting it with a particular color, just as the drawer icon/back button can be tinted via the color attribute in drawerArrowStyle.
I have tried these methods, which reportedly used to work:
Setting colorControlNormal -- Toolbar icon tinting on Android
Setting textColorPrimary in the theme -- MenuItem tinting on AppCompat Toolbar
Setting a custom actionBarStyle and changing colorControlNormal there.
But as far as I can see none of them work with the latest AppCompat -- the overflow icon keeps its original color (while other widgets, such as TextViews or the text in menu items do change).
Should this be done differently now? I am not using a custom Toolbar view, just the default AppCompat-provided, ActionBar-like one.
How to reproduce:
Create a default Android Studio project, with minimum SDK version = 9.
This automatically includes a blank activity and a menu resource with a single menu item, "Settings", with app:showAsAction="never" which means it will be displayed in the overflow menu.
Finally, customize the styles.xml file. For example:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#008888</item>
<item name="colorControlNormal">#ff0000</item>
<item name="android:textColorPrimary">#ff0000</item>
</style>
</resources>
You'll notice that neither property affects the overflow menu icon color. Tested in a Nexus 5 with Android 5.1.1.
If you trace the Theme.AppCompat.Light.DarkActionBar theme, you see that it sets:
<item name="actionBarTheme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
Which in turn inherits from Base.ThemeOverlay.AppCompat.Dark, which sets:
<item name="android:textColorPrimary">#color/abc_primary_text_material_dark</item>
Which essentially equals white:
<color name="primary_text_default_material_dark">#ffffffff</color>
If you just inherit from Base.Theme.AppCompat.Light, you won't run into this issue since it doesn't set the actionBarTheme. You can easily customize the action bar using the material properties anyway.
I am updating my application to use the version 22.1.1 of the Android Support Library. My application theme inherits from Theme.AppCompat.Light.DarkActionBar.
It works fine, except that all texts are white if the TextView style is set to one of the predefined style. So I end up with white texts on light background (default background color). With version 22.0.0, I had no issue.
I tried the follow in my theme, but it does not seem to work:
<item name="android:textColor">#color/black</item>
<item name="android:textColorPrimary">#color/black</item>
<item name="android:textColorPrimaryInverse">#color/black</item>
Moreover, the ActionMode now has a black background instead of white, as it used to be with 22.0.0.
Any idea on how to change this?
I was using the theme attribute in my theme to set the Toolbar theme. That's a mistake. The Toolbar theme has to be defined on the Toolbar tag directly.
For the ActionMode background color, it looks like I was taking profit of a bug without knowing it.
I'm using Toolbar as setSupportActionBar(toolbar). How i can change action mode background color. I try in my App Theme change attribute:
<item name="android:actionModeBackground">#color/color</item>
But it is not working. How can I solve this problem?
The AppCompat library supports versions of Android that don't have ActionBars, so it needs to define attributes to use that wouldn't exist on those devices. You want to use this:
<item name="actionModeBackground">#color/color</item>
The same goes for any other style/theme attributes you want to use that have been backported. But you'd use platform ones that exist, like android:textColor or android:background.