Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Styles, Themes, Material Theming, Oh My!

Anita
March 27, 2019

Styles, Themes, Material Theming, Oh My!

Link to the talk:
https://youtu.be/oSSi50Qv9DE

Here are the links mentioned in the slides :

https://issuetracker.google.com/issues?q=componentid:439535 status:open
https://www.material.io/develop/android/docs/theming-guide/
https://github.com/material-components
https://material.io/tools/theme-editor/
https://medium.com/androiddevhttps://speakerdeck.com/cyrilmottier/themes-styles-and-more-from-zero-to-hero?slide=103elopers/whats-your-text-s-appearance-f3a1729192d
https://www.youtube.com/watch?v=TIHXGwRTMWI
https://medium.com/over-engineering/setting-up-a-material-components-theme-for-android-fbf7774da739
https://www.youtube.com/watch?v=DPH3F0v1jB0

Abstract:

You want to style the title of dialogs throughout your app and you think it should be simple enough. However, once you dive into it, you might start asking questions - should I use alertDialogTheme or alertDialogStyle? Should I use dialogTitle, windowTitle or textAppearanceLarge? It is no secret that the styling and theming system in Android is complex, but I promise there is some method to the madness! We can avoid writing custom components just to achieve pixel perfection if we know how tame to the beast.

In this talk, we will learn about the difference between themes and styles, the hierarchy of different styling techniques and how to use them effectively. We will also discuss migrating to the newest app theme in town - the Material Components Theme and using the new attributes that come with it, like “colorOnSurface”.

At the end of this talk, you will know how to style your app like a pro with the correct use of Styles, Themes, TextAppearance and sensible defaults while specifying color, shape and typography through Material Theming.

Talk given at Berlindroid March 26th, 2019, Droidcon Italy April 5th, 2019.

Anita

March 27, 2019
Tweet

More Decks by Anita

Other Decks in Technology

Transcript

  1. Example <Button .. style=“@style/WhiteTextButtonWidget" .. /> <style name=“WhiteTextButtonWidget" parent=“Widget.MaterialComponents.Button"> <item

    name="android:textColor">@color/white</item> <item name="android:fontFamily">@font/cera_pro_bold</item> </style>
  2. When? • You have a bloated style with text attributes

    • You are only styling text at the character level
  3. Example Default Style Attribute <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="colorPrimary">@color/white</item> <item

    name=“colorPrimaryVariant">@color/midnight</item> <item name="colorSecondary">@color/blinkist_green</item> <item name="colorControlNormal">@color/midnight</item> <item name=“cardViewStyle”>@style/RoundCardViewStyle</item> … </style>
  4. Example <application android:theme=“@style/AppTheme" … /> <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="colorPrimary">@color/white</item>

    <item name="colorPrimaryDark">@color/midnight</item> <item name="colorAccent">@color/blinkist_green</item> <item name="colorControlNormal">@color/midnight</item> <item name=“cardViewStyle”>@style/RoundCardViewStyle</item> … </style>
  5. Example <style name="ThemeOverlay.MaterialComponents"/> <style name="ThemeOverlay.MaterialComponents.ActionBar"/> <style name="ThemeOverlay.MaterialComponents.Dark"/> <style name="ThemeOverlay.MaterialComponents.Dark.ActionBar"/> <style

    name="ThemeOverlay.MaterialComponents.Dialog"/> <style name="ThemeOverlay.MaterialComponents.Dialog.Alert"/> <style name="ThemeOverlay.MaterialComponents.TextInputEditText"/>
  6. When? • Overlay an existing theme by changing only what

    you need • Invert the theme for a view or view hierarchy
  7. Example Default Style Attribute <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="colorPrimary">@color/white</item> <item

    name=“colorPrimaryVariant">@color/midnight</item> <item name="colorSecondary">@color/blinkist_green</item> <item name="colorControlNormal">@color/midnight</item> <item name=“cardViewStyle”>@style/RoundCardViewStyle</item> … </style>
  8. How? public CardView(@NonNull Context context, @Nullable AttributeSet attrs) { this(context,

    attrs, R.attr.cardViewStyle); } public CardView(@NonNull Context context, @Nullable AttributeSet attrs, 
 int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CardView, defStyleAttr, R.style.CardView);
 …
  9. How? public CardView(@NonNull Context context, @Nullable AttributeSet attrs) { this(context,

    attrs, R.attr.cardViewStyle); } public CardView(@NonNull Context context, @Nullable AttributeSet attrs, 
 int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CardView, defStyleAttr, R.style.CardView);
 …
  10. How? TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CardView, defStyleAttr, R.style.CardView); public CardView(@NonNull

    Context context, @Nullable AttributeSet attrs) { this(context, attrs, R.attr.cardViewStyle); } public CardView(@NonNull Context context, @Nullable AttributeSet attrs, 
 int defStyleAttr) { super(context, attrs, defStyleAttr);
  11. How? TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CardView, defStyleAttr, R.style.CardView) public CardView(@NonNull

    Context context, @Nullable AttributeSet attrs) { this(context, attrs, R.attr.cardViewStyle); } public CardView(@NonNull Context context, @Nullable AttributeSet attrs, 
 int defStyleAttr) { super(context, attrs, defStyleAttr);
  12. Color theming: new attributes <item name="colorPrimaryVariant">..</item> <item name=“colorOnPrimary">..</item> <item name="colorSecondary">..</item>

    <item name="colorSecondaryVariant">..</item> <item name="colorOnSecondary">..</item> <item name="colorSurface">..</item> <item name="colorOnSurface">..</item> <item name=“colorOnBackground">..</item> <item name="colorOnError">..</item>
  13. New attributes: shape <style ..parent="ShapeAppearance.MaterialComponents.S mallComponent"> <item name="cornerFamilyTopLeft">cut</item> <item name="cornerFamilyTopRight">cut</item>

    <item name="cornerFamilyBottomLeft">rounded</item> <item name="cornerFamilyBottomRight">rounded</item> <item name="cornerSize">8dp</item> </style> <item name=“shapeAppearanceMediumComponent”>..</item> <item name=“shapeAppearanceLargeComponent”>..</item> <item name=“shapeAppearanceSmallComponent">..</item>
  14. New attributes: shape <item name=“shapeAppearanceMediumComponent”>..</item> <item name=“shapeAppearanceLargeComponent”>..</item> <item name=“shapeAppearanceSmallComponent">..</item> <style

    ..parent="ShapeAppearance.MaterialComponents .SmallComponent"> <item name="cornerFamilyTopLeft">cut</item> <item name="cornerFamilyTopRight">cut</item> <item name="cornerFamilyBottomLeft">rounded</item> <item name="cornerFamilyBottomRight">rounded</item> <item name="cornerSize">8dp</item> </style>
  15. New attributes: typography <item name="textAppearanceHeadline1"></item> <item name="textAppearanceHeadline2"></item> <item name="textAppearanceHeadline3"></item> <item

    name="textAppearanceHeadline4"></item> <item name="textAppearanceHeadline5"></item> <item name="textAppearanceHeadline6"></item> <item name="textAppearanceSubtitle1"></item> <item name="textAppearanceSubtitle2"></item> <item name=“textAppearanceBody1”></item> <item name="textAppearanceBody2"></item> <item name="textAppearanceCaption"></item> <item name=“textAppearanceButton"></item> <item name="textAppearanceOverline"></item> …
  16. android:background="@color/blinkist_green" Migrating to Material Component Theme <style name="ButtonStyle" parent="Widget.MaterialComponents.Button"> <item

    name="backgroundTint">@color/button_selector</item> … </style> android:background="@color/blinkist_green"
  17. Migrating to Material Component Theme <style name="ButtonStyle" parent="Widget.MaterialComponents.Button"> <item name="backgroundTint">@color/button_selector</item>

    … </style> … <item name=“materialButtonStyle">@style/ButtonStyle</item> … android:background="@color/blinkist_green"
  18. Color of button background = colorPrimary Option 2 : theme

    overlay set on button! Option 3 : materialThemeOverlay set on default style! Migrating to Material Component Theme
  19. <style name="ButtonStyle" parent="Widget.MaterialComponents.Button"> <item name=“materialThemeOverlay”>@style/ MidnightButtonThemeOverlay</item> … </style> …
 <item

    name=“materialButtonStyle">@style/ButtonStyle</item> … Migrating to Material Component Theme <style name="MidnightButtonThemeOverlay"> <item name="colorPrimary">@color/midnight</item> </style>
  20. Migrating to Material Component Theme <style name=“…” parent="Widget.MaterialComponents.Button.Icon"> <item name="backgroundTint">@color/white</item>

    <item name="icon">@drawable/ic_google</item> <item name="iconTint">@null</item> <item name="android:textColor">@color/mid_grey</item> </style>
  21. Migrating to Material Component Theme <style name=“…” parent="Widget.MaterialComponents.Button.Icon"> <item name="backgroundTint">@color/white</item>

    <item name="icon">@drawable/ic_google</item> <item name="iconTint">@null</item> <item name="android:textColor">@color/mid_grey</item> </style>
  22. Color of button text = colorPrimary Migrating to Material Component

    Theme to <style name="AlertDialogTheme" parent=“Theme.MaterialComponent.Light.Dialog.Alert"> <style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
  23. Color of selection = colorPrimary Migrating to Material Component Theme

    .. <item name="textInputStyle">@style/ Widget.MaterialComponents.TextInputLayout.OutlinedBox</item> ..
  24. If something looks funny… • Find the attribute(s) that matter

    • What is the attribute being applied as – theme? style? • Order of Precedence. What could be overriding it? • Have it extend a MaterialComponent style, if present • There are bugs!
  25. Useful Links • Material Theming Guide • Material Components Github

    • Material Theme Editor • What's Your Text Appearance? • Themes, Styles And More From Zero To Hero • Android Themes & Styles Demystified • Setting Up Material Components Theme For Android • The Components of Material Design