I've started to replace styles with the new Material3 ones (Material You) which are present in the latest Material Design Components library. However, I don't see a replacement for Widget.MaterialComponents.Toolbar.PrimarySurface (a style that handles dark mode better by not using the primary color, which looks to bright). There's Widget.Material3.Toolbar, Widget.Material3.Toolbar.Surface and Widget.Material3.Toolbar.OnSurface but I don't think any of them is a replacement.
Am I missing something here?
Widget.MaterialComponents.Toolbar.PrimarySurface style is just a combination of Widget.MaterialComponents.Toolbar.Primary and Widget.MaterialComponents.Toolbar.Surface styles for light and dark mode.
That means this style uses colorPrimary in light mode and colorSurface in dark mode.
In the Material 3 (Material you) , the default background color for the toolbar is changed from colorPrimary to colorSurface.
You can always customize these material 3 themes according to your preference.
Related
I am migrating from AppCompat to MaterialComponents, and I would like to keep some widgets (like the Bottom Navigation Bar) AppCompat-themed. However, when I want to apply MaterialComponents theming to Buttons and TextFields, I must set my app theme to MaterialComponents.... and therefore all my widgets are MaterialComponents-themed. How can I make only some widgets MaterialComponents-themed ? I have been looking for an answer on StackOverflow but couldn't find anything.
You can use Bridge themes like :
Theme.MaterialComponents.Bridge
Theme.MaterialComponents.Light.Bridge
Theme.MaterialComponents.NoActionBar.Bridge
Theme.MaterialComponents.Light.NoActionBar.Bridge
Theme.MaterialComponents.Light.DarkActionBar.Bridge
Bridge themes inherit from AppCompat themes, but also define the new Material Components theme attributes for you. If you use a bridge theme, you can start using Material Design components without changing your app theme. (From documentaion)
Have a look : Get started - Material Design
Referring to the following Material design Guide
I want to modify the 12 different colors in my app e.g. Primary Variant, Secondary, etc.
However, in Android studio, when using the Theme Editor, the list of colors available to modify are different than what is specified in the material design guide. This is true no matter which theme I choose.
The 12 color variants as defined by both the Material Design and in the Theme Editor in android studio are as follows:
Material Design:
colorPrimary
colorPrimaryVariant
colorSecondary
colorSecondaryVariant
colorBackground
colorError
colorSurface
colorOnPrimary
colorOnSecondary
colorOnBackground
colorOnError
colorOnSurface
Theme Editor:
colorPrimary
colorPrimaryDark
colorAccent
android:colorBackground
android:colorForeground
android:navigationBarColor
android:statusBarColor
android:textColorPrimary
android:textColorPrimaryInverse
android:textColorSecondary
android:textColorSecondaryInverse
android:windowBackground
Are these colors just different names for the same thing or am I just looking in the incorrect place? I couldn't find anything in the material design documentation that that had names similar to what was in android studio so I'm a bit unsure.
Any guidance would be greatly appreciated!
The new colors attributes are not available on current stable version 1.0.0, however are available in latest 1.1.0 alpha:
implement "com.google.android.material:material:1.1.0-alpha05"
The documentation is ahead of implementation. You can check the release logs to have an idea:
https://github.com/material-components/material-components-android/releases
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()
}
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'm trying to create transitions in my application and it looks bad since the background color of my "android:windowBackground" (white) is flashing for a few ms when I change activity.
Managed to remove it with the Translucent Theme (parent="android:Theme.Translucent.NoTitleBar"), but Translucent does not seem to be an option when using the Material theme. Is there any way to combine those themes?