Slide 1

Slide 1 text

Themes, styles & more From zero to hero @cyrilmottier

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Yes, I’ll talk about boring XML while everybody talks about the trendy stuff like Kotlin. “

Slide 5

Slide 5 text

Yes, I’ll talk about boring XML while everybody talks about the trendy stuff like Kotlin. Sorry. “ – The guy talking to you

Slide 6

Slide 6 text

AAPT 2 \eɪeɪpiːtiː ˈtuː\ AAPT stands for Android Asset Packaging Tool. It compiles application resources (e.g. AndroidManifest.xml, XML files, etc.) into binary assets. AAPT also allow clients to view, create, and update Android archives.

Slide 7

Slide 7 text

The mother of all A key-value store with zero or more pairs

Slide 8

Slide 8 text

The mother of all A key-value store with zero or more pairs

Slide 9

Slide 9 text

<item name="android:minHeight"> @dimen/grid_size_300 </item>

Slide 10

Slide 10 text

<item name="android:minHeight"> @dimen/grid_size_300 </item> Style’s name in upper camel case name="ListItem"

Slide 11

Slide 11 text

<item name="android:minHeight"> @dimen/grid_size_300 </item> Key-value pair @dimen/grid_size_300

Slide 12

Slide 12 text

<item name="android:minHeight"> @dimen/grid_size_300 </item> name="android:minHeight" Key

Slide 13

Slide 13 text

<item name="android:minHeight"> @dimen/grid_size_300 </item> @dimen/grid_size_300 Value

Slide 14

Slide 14 text

Keys in styles are called attributes

Slide 15

Slide 15 text

Carry type information Belong to a namespace Hold optional constraints Keys in styles are called attributes

Slide 16

Slide 16 text

”any” by default (i.e. when undefined) Attributes Format types

Slide 17

Slide 17 text

boolean color dimension float string integer enum flags reference fraction Attributes Format types

Slide 18

Slide 18 text

boolean color dimension float string integer enum flags reference fraction Resource types Attributes Format types

Slide 19

Slide 19 text

boolean color dimension float string integer enum flags reference fraction Special types Attributes Format types

Slide 20

Slide 20 text

Attribute namespace Android framework namespace

Slide 21

Slide 21 text

Attribute namespace Android framework namespace xmlns:android="http://schemas.android.com/apk/res/android"

Slide 22

Slide 22 text

Attribute namespace Android framework namespace android android android

Slide 23

Slide 23 text

Attribute namespace Global namespace schema

Slide 24

Slide 24 text

Attribute namespace Global namespace schema xmlns:app="http://schemas.android.com/apk/" app

Slide 25

Slide 25 text

Attribute namespace Global namespace schema xmlns:app="http://schemas.android.com/apk/res-auto" app

Slide 26

Slide 26 text

Attribute namespace Global namespace schema xmlns:app="http://schemas.android.com/apk/res-auto" Type “appNs” in Android Studio app

Slide 27

Slide 27 text

Slide 28

Slide 28 text

name="background" Attribute’s name

Slide 29

Slide 29 text

Pipe-separated list of types format="reference|color"

Slide 30

Slide 30 text

Slide 31

Slide 31 text

Upper/lower bounds only with integer typed attributes min="1" max="12"

Slide 32

Slide 32 text

Slide 33

Slide 33 text

Android comes with a bunch of attributes

Slide 34

Slide 34 text

textColor actionBarStyle windowReturnTransition text src Naming General convention

Slide 35

Slide 35 text

textColor actionBarStyle windowReturnTransition text src Naming General convention Lower camel case

Slide 36

Slide 36 text

Naming Exc. #1: layout attributes layout_width layout_alignBaseline layout_marginHorizonal layout_weight layout_x

Slide 37

Slide 37 text

Naming Exc. #1: layout attributes layout_width layout_alignBaseline layout_marginHorizonal layout_weight layout_x Prefixed with “layout_” then lower camel case

Slide 38

Slide 38 text

state_focused state_checked state_enabled state_drag_hovered state_window_focused Naming Exc. #2: state attributes

Slide 39

Slide 39 text

state_focused state_checked state_enabled state_drag_hovered state_window_focused Naming Exc. #2: state attributes Prefixed with “state_” then snake case

Slide 40

Slide 40 text

Learn the rules like a pro, so you can break them like an artist. “ – Pablo Picasso

Slide 41

Slide 41 text

hand_hour hand_minute ConstraintLayout Readability and ease of use AnalogClock Legacy & deprecated layout_constraintGuide_end layout_constraintHeight_max layout_constraintTop_toTopOf

Slide 42

Slide 42 text

Styles can inherit from each other creating a style hierarchy

Slide 43

Slide 43 text

Slide 44

Slide 44 text

Slide 45

Slide 45 text

Slide 46

Slide 46 text

Style.Wars"

Slide 47

Slide 47 text

Slide 48

Slide 48 text

Implicit parent Style.Wars

Slide 49

Slide 49 text

Slide 50

Slide 50 text

Namespaced parent android:

Slide 51

Slide 51 text

Slide 52

Slide 52 text

Parentless style parent=""

Slide 53

Slide 53 text

Shorter is better No parent | Implicit parent | Shorter parent form

Slide 54

Slide 54 text

is a universe of styles Android

Slide 55

Slide 55 text

Text appearances is a universe of styles Android Themes Theme overlays Styles

Slide 56

Slide 56 text

Style A bag of attributes used to style a View

Slide 57

Slide 57 text

Slide 58

Slide 58 text

android:background="@drawable/list_selector" android:minHeight="@dimen/grid_size_400" android:paddingBottom="@dimen/spacing_100" android:paddingLeft="@dimen/spacing_200" android:paddingRight="@dimen/spacing_200" android:paddingTop="@dimen/spacing_100"

Slide 59

Slide 59 text

<item name="android:background">@drawable/list_selector</item> <item name="android:minHeight">@dimen/grid_size_400</item> <item name="android:paddingBottom">@dimen/spacing_100</item> <item name="android:paddingLeft">@dimen/spacing_200</item> <item name="android:paddingRight">@dimen/spacing_200</item> <item name="android:paddingTop">@dimen/spacing_100</item>

Slide 60

Slide 60 text

Slide 61

Slide 61 text

Slide 62

Slide 62 text

Slide 63

Slide 63 text

Slide 64

Slide 64 text

TextAppearance Text styling

Slide 65

Slide 65 text

<item name=“android:fontFamily"> @font/museo_sans_rounded_500 </item> <item name="android:textColor">@color/blue_dark</item> <item name="android:textSize">@dimen/font_size_large</item>

Slide 66

Slide 66 text

<item name=“android:fontFamily"> @font/museo_sans_rounded_500 </item> <item name="android:textColor">@color/blue_dark</item> <item name="android:textSize">@dimen/font_size_large</item> TextAppearance. Prefixed by “TextAppearance.”

Slide 67

Slide 67 text

val tv = findViewById(R.id.text) tv.setTextAppearance(R.style.TextAppearance_Title)

Slide 68

Slide 68 text

val tv = findViewById(R.id.text) tv.setTextAppearance(R.style.TextAppearance_Title) Modify TextView’s text appearance tv.setTextAppearance(R.style.TextAppearance_Title)

Slide 69

Slide 69 text

val spannable = SpannableString("Styles rock!") spannable.setSpan( TextAppearanceSpan(context, R.style.TextAppearance_Title), 0, spannable.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) textView.text = spannable

Slide 70

Slide 70 text

val spannable = SpannableString("Styles rock!") spannable.setSpan( TextAppearanceSpan(context, R.style.TextAppearance_Title), 0, spannable.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) textView.text = spannable TextAppearanceSpan(context, R.style.TextAppearance_Title) Create a Span using the given text appearance

Slide 71

Slide 71 text

TextAppearance Supported attributes textColor textSize textStyle typeface fontFamily textColorHighlight textColorHint textColorLink textAllCaps shadowColor shadowDx shadowDy shadowRadius elegantTextHeight letterSpacing fontFeatureSettings

Slide 72

Slide 72 text

TextAppearance TextAppearanceSpan attributes textColor textSize textStyle typeface fontFamily textColorHighlight textColorHint textColorLink textAllCaps shadowColor shadowDx shadowDy shadowRadius elegantTextHeight letterSpacing fontFeatureSettings

Slide 73

Slide 73 text

Slide 74

Slide 74 text

android:textAppearance="@style/TextAppearance.Title" Set TextView’s text appearance

Slide 75

Slide 75 text

android:textAppearance="@style/TextAppearance.Title" Least priority (applied first)

Slide 76

Slide 76 text

style="@style/FormHeader" Applied then

Slide 77

Slide 77 text

android:textColor="@color/blue_cerulean" Highest priority

Slide 78

Slide 78 text

Theme Activity-level styling

Slide 79

Slide 79 text

Theme Activity-level styling fun Context.setTheme(resId: Int) val Context.theme: Resources.Theme

Slide 80

Slide 80 text

Theme Activity-level styling fun Context.setTheme(resId: Int) val Context.theme: Resources.Theme fun Context.setTheme(resId: Int) Apply the theme to the receiver’s Context

Slide 81

Slide 81 text

Theme Activity-level styling fun Context.setTheme(resId: Int) val Context.theme: Resources.Theme fun Context.setTheme(resId: Int) Must be called before any View is inflated ⚠

Slide 82

Slide 82 text

Slide 83

Slide 83 text

android:theme="@style/Theme.City" Application’s default theme

Slide 84

Slide 84 text

Slide 85

Slide 85 text

Activity specific theme android:theme="@style/Theme.City.Deluxe"

Slide 86

Slide 86 text

Colors android:colorPrimary android:textColorPrimary

Slide 87

Slide 87 text

Window configuration android:windowBackground android:windowDisablePreview Colors android:colorPrimary android:textColorPrimary

Slide 88

Slide 88 text

Window configuration android:windowBackground android:windowDisablePreview Colors android:colorPrimary android:textColorPrimary Default widget styles android:textViewStyle android:progressBarStyle

Slide 89

Slide 89 text

Window configuration android:windowBackground android:windowDisablePreview Colors android:colorPrimary android:textColorPrimary Default widget styles android:textViewStyle android:progressBarStyle Drawables android:selectableItemBackground android:listDivider

Slide 90

Slide 90 text

Drawables android:selectableItemBackground android:listDivider Preferences styles android:switchPreferenceStyle android:editTextPreferenceStyle Window configuration android:windowBackground android:windowDisablePreview Colors android:colorPrimary android:textColorPrimary Default widget styles android:textViewStyle android:progressBarStyle

Slide 91

Slide 91 text

Text appearances android:textAppearanceSmall android:textAppearanceLargeInverse Preferences styles android:switchPreferenceStyle android:editTextPreferenceStyle Drawables android:selectableItemBackground android:listDivider Window configuration android:windowBackground android:windowDisablePreview Colors android:colorPrimary android:textColorPrimary Default widget styles android:textViewStyle android:progressBarStyle

Slide 92

Slide 92 text

Text appearances android:textAppearanceSmall android:textAppearanceLargeInverse Preferences styles android:switchPreferenceStyle android:editTextPreferenceStyle Misc. android:dialogTheme android:disabledAlpha Drawables android:selectableItemBackground android:listDivider Window configuration android:windowBackground android:windowDisablePreview Colors android:colorPrimary android:textColorPrimary Default widget styles android:textViewStyle android:progressBarStyle

Slide 93

Slide 93 text

Theme attributes can be referenced in layouts, styles, etc.

Slide 94

Slide 94 text

<item name=“android:background”> @drawable/list_selector </item> <item name="android:minHeight">@dimen/grid_size_400</item> <item name="android:paddingBottom">@dimen/spacing_100</item> <item name="android:paddingLeft">@dimen/spacing_200</item> <item name="android:paddingRight">@dimen/spacing_200</item> <item name="android:paddingTop">@dimen/spacing_100</item>

Slide 95

Slide 95 text

<item name=“android:background”> ?android:attr/selectableItemBackground </item> <item name="android:minHeight">@dimen/grid_size_400</item> <item name="android:paddingBottom">@dimen/spacing_100</item> <item name="android:paddingLeft">@dimen/spacing_200</item> <item name="android:paddingRight">@dimen/spacing_200</item> <item name="android:paddingTop">@dimen/spacing_100</item>

Slide 96

Slide 96 text

?android:attr/selectableItemBackground

Slide 97

Slide 97 text

Indicates a theme lookup ?android:attr/selectableItemBackground ?

Slide 98

Slide 98 text

Theme attribute namespace ?android:attr/selectableItemBackground android:

Slide 99

Slide 99 text

Attribute lookup ?android:attr/selectableItemBackground attr/

Slide 100

Slide 100 text

Theme attribute actual name ?android:attr/selectableItemBackground selectableItemBackground

Slide 101

Slide 101 text

?android:selectableItemBackground

Slide 102

Slide 102 text

?colorPrimary

Slide 103

Slide 103 text

fun Context.obtainStyledAttributes( set: AttributeSet, attrs: IntArray, defStyleAttr: Int, defStyleRes: Int ): TypedArray How it works Theming is not so magical after all…

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

Theme overlays View-level theming

Slide 106

Slide 106 text

Slide 107

Slide 107 text

Override the current theme with the given theme overlay android:theme="@style/ThemeOverlay.Bazinga"

Slide 108

Slide 108 text

Few notes about theme overlays Based on ContextThemeWrapper A bag of theme attributes Propagated to descendants

Slide 109

Slide 109 text

Compatibility Theming on different API levels

Slide 110

Slide 110 text

with AppCompat Theme overlays API 21 API 14 API 23

Slide 111

Slide 111 text

with AppCompat Theme overlays Theme attributes API 21 API 14 API 23

Slide 112

Slide 112 text

with AppCompat Theme overlays Theme attributes in layout & styles API 21 API 14 API 23

Slide 113

Slide 113 text

with AppCompat Theme overlays Theme attributes in layout & styles in Drawables API 21 API 14 VD & AVD only API 23

Slide 114

Slide 114 text

with AppCompat Theme overlays Theme attributes in layout & styles in Drawables in ColorStateList API 21 API 14 with AppCompat with AppCompat VD & AVD only API 23

Slide 115

Slide 115 text

Theming & tinting in the real life

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

layout/list_item_user.xml

Slide 121

Slide 121 text

layout/list_item_user.xml android:textColor="?zenlyListItemTextColorPrimary" android:textColor="?zenlyListItemTextColorSecondary"

Slide 122

Slide 122 text

layout/fragment_dashboard.xml

Slide 123

Slide 123 text

layout/fragment_dashboard.xml android:background="?selectableItemBackgroundBorderless" app:tint="?colorControlNormal" />

Slide 124

Slide 124 text

values/themes.xml values/theme_overlays.xml <item name=“colorControlNormal"> @color/blue_dark </item> <item name=“zenlyListItemTextColorPrimary"> @color/white </item> <item name=“zenlyListItemTextColorSecondary"> @color/blue_heavy </item> <item name=“android:listDivider"> @drawable/divider_blue_medium </item> <item name="colorControlNormal"> @color/gray_medium </item> <item name="zenlyListItemTextColorPrimary"> @color/blue_dark </item> <item name="zenlyListItemTextColorSecondary"> @color/gray_medium </item> <item name="android:listDivider"> @drawable/divider_gray_light </item>

Slide 125

Slide 125 text

<item name="colorControlNormal"> @color/gray_medium </item> <item name="zenlyListItemTextColorPrimary"> @color/blue_dark </item> <item name="zenlyListItemTextColorSecondary"> @color/gray_medium </item> <item name="android:listDivider"> @drawable/divider_gray_light </item> values/themes.xml values/theme_overlays.xml <item name=“colorControlNormal"> @color/blue_dark </item> <item name=“zenlyListItemTextColorPrimary"> @color/white </item> <item name=“zenlyListItemTextColorSecondary"> @color/blue_heavy </item> <item name=“android:listDivider"> @drawable/divider_blue_medium </item> @color/gray_medium @color/blue_dark @color/gray_medium @drawable/divider_gray_light @color/blue_dark @color/white @color/blue_heavy @drawable/divider_blue_medium

Slide 126

Slide 126 text

layout/fragment_dashboard.xml

Slide 127

Slide 127 text

layout/fragment_dashboard.xml android:theme="@style/ThemeOverlay.Zenly.Light"

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

Normal mode Night mode

Slide 130

Slide 130 text

values/themes.xml <item name="colorPrimary">@color/blue</item> <item name="colorPrimaryDark">@color/blue_dark</item> <item name="colorAccent">@color/green</item> <item name="colorControlNormal">@color/blue_dark</item> <item name="zenlyColorBackground">@color/blue</item> <item name="zenlyListItemTextColorPrimary">@color/white</item> <item name="zenlyListItemTextColorSecondary">@color/blue_heavy</item> <!-- ... -->

Slide 131

Slide 131 text

values-night/themes.xml <item name="colorPrimary">@color/blue_dark</item> <item name="colorPrimaryDark">@color/blue_black</item> <item name="colorAccent">@color/green</item> <item name="colorControlNormal">@color/blue_heavy</item> <item name="zenlyColorBackground">@color/blue_dark</item> <item name="zenlyListItemTextColorPrimary">@color/white</item> <item name="zenlyListItemTextColorSecondary">@color/white_o50</item> <!-- ... -->

Slide 132

Slide 132 text

@Override protected void onCreate(@Nullable Bundle savedInstanceState) { final int uiMode = AppPrefs.from(this).getUiMode(); if (uiMode == AppPrefs.UI_MODE_NIGHT) { getDelegate().setLocalNightMode( AppCompatDelegate.MODE_NIGHT_YES); } else { getDelegate().setLocalNightMode( AppCompatDelegate.MODE_NIGHT_NO); } super.onCreate(savedInstanceState); // ... }

Slide 133

Slide 133 text

Organizing styles Good practices for XML clean architecture

Slide 134

Slide 134 text

<item name="android:navigationBarColor">@color/black_o30</item> <item name="android:statusBarColor">@color/black_o30</item> <item name="android:windowBackground">@color/white</item> <item name="android:windowTranslucentNavigation">true</item> values/themes.xml

Slide 135

Slide 135 text

<item name="android:navigationBarColor">@color/black_o30</item> <item name="android:statusBarColor">@color/black_o30</item> <item name="android:windowBackground">@color/white</item> <item name="android:windowTranslucentNavigation">true</item> @color/black_o30 @color/black_o30 true values/themes.xml Lint warnings

Slide 136

Slide 136 text

<item name="android:windowBackground">@color/white</item> values/themes.xml <item name="android:navigationBarColor">@color/black_o30</item> <item name="android:statusBarColor">@color/black_o30</item> <item name="android:windowBackground">@color/white</item> <item name="android:windowTranslucentNavigation">true</item> values-v21/themes.xml

Slide 137

Slide 137 text

<item name="android:windowBackground">@color/white</item> values/themes.xml <item name="android:navigationBarColor">@color/black_o30</item> <item name="android:statusBarColor">@color/black_o30</item> <item name="android:windowBackground">@color/white</item> <item name="android:windowTranslucentNavigation">true</item> parent="Theme.AppCompat.Light.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar" Parent style duplication values-v21/themes.xml

Slide 138

Slide 138 text

<item name="android:windowBackground">@color/white</item> values/themes.xml <item name="android:navigationBarColor">@color/black_o30</item> <item name="android:statusBarColor">@color/black_o30</item> <item name="android:windowBackground">@color/white</item> <item name="android:windowTranslucentNavigation">true</item> Items duplication @color/white @color/white values-v21/themes.xml

Slide 139

Slide 139 text

Theme.City values values-v21 values-v23 Base.Theme.City Base.Theme.City Base.Theme.City Base.V23.Theme.City Base.V21.Theme.City Base.V1.Theme.City

Slide 140

Slide 140 text

Theme.City values values-v21 values-v23 Base.Theme.City Base.Theme.City Base.Theme.City Base.V23.Theme.City Base.V21.Theme.City Base.V1.Theme.City

Slide 141

Slide 141 text

Theme.City values values-v21 values-v23 Base.Theme.City Base.Theme.City Base.Theme.City Base.V23.Theme.City Base.V21.Theme.City Base.V1.Theme.City

Slide 142

Slide 142 text

Theme.City values values-v21 values-v23 Base.Theme.City Base.Theme.City Base.Theme.City Base.V23.Theme.City Base.V21.Theme.City Base.V1.Theme.City

Slide 143

Slide 143 text

Theme.City values values-v21 values-v23 Base.Theme.City Base.Theme.City Base.Theme.City Base.V23.Theme.City Base.V21.Theme.City Base.V1.Theme.City

Slide 144

Slide 144 text

Theme.City values values-v21 values-v23 Base.Theme.City Base.Theme.City Base.Theme.City Base.V23.Theme.City Base.V21.Theme.City Base.V1.Theme.City on API 22

Slide 145

Slide 145 text

Theme.City values values-v21 values-v23 Base.Theme.City Base.Theme.City Base.Theme.City Base.V23.Theme.City Base.V21.Theme.City Base.V1.Theme.City on API 26

Slide 146

Slide 146 text

values/themes_base.xml <item name="android:windowBackground">@color/white</item>

Slide 147

Slide 147 text

<item name="android:navigationBarColor">@color/black_o30</item> <item name="android:statusBarColor">@color/black_o30</item> <item name="android:windowTranslucentNavigation">true</item> values-v21/themes_base.xml

Slide 148

Slide 148 text

values/themes.xml

Slide 149

Slide 149 text

Prefix Filename text_appearances.xml themes.xml theme_overlays.xml TextAppearance. Theme. ThemeOverlay. Text appearances Themes Theme overlays styles_widget.xml Widget. Widget styles styles.xml - Custom styles

Slide 150

Slide 150 text

Prefix Filename text_appearances_base.xml themes_base.xml theme_overlays_base.xml Base.TextAppearance. Base.Theme. Base.ThemeOverlay. Base text appearances Base themes Base theme overlays styles_widget_base.xml Base.Widget. Base widget styles styles_base.xml Base. Base custom styles

Slide 151

Slide 151 text

Thank you! @cyrilmottier cyrilmottier.com