Share
Embed
Transcript
94 slides
-
Kyash • I work for Japanese Fintech company called Kyash.
• It makes easy to create Visa card and send money to anyone via app! https://play.google.com/store/apps/details?id=co.kyash -
Android Theme Overview • Technically the same as Style. •
Theme is “Global”, Style is “Local”. • It’s important to use Theme especially since Material Design appeared. -
Today I’ll talk about 1. Apply theme - Which parent
theme should we implement? - How should we inherit and manage themes? - How can we change theme and night mode dynamically? 2. Theme attributes - Which attributes affect where? - How can we find all the attributes for AppCompat and Material Components? - Which should we apply theme or style? -
Today I’ll talk about 1. Apply theme - Which parent
theme should we implement? - How should we inherit and manage themes? - How can we change theme and night mode dynamically? 2. Theme attributes - Which attributes affect where? - How can we find all the attributes for AppCompat and Material Components? - Which should we apply theme or style? -
Today I’ll talk about 1. Apply theme - Which parent
theme should we implement? - How should we inherit and manage themes? - How can we change theme and night mode dynamically? 2. Theme attributes - Which attributes affect where? - How can we find all the attributes for AppCompat and Material Components? - Which should we apply theme or style? -
Inheritance <style name="Theme.AppCompat.Light" parent="Base.Theme.AppCompat.Light"/> <style name="Theme.AppCompat.Light.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
</style> parent (higher priority than dot style) dot Theme.AppCompat.Light.NoActionBar inherites Theme.AppCompat.Light with some attributes which is related to ActionBar. -
When should we use parent inheritance? • Inherit the theme
which is provided by the library like AppCompat, MaterialComponents. • Manage the API version differences. -
<style name="Base.Theme.AppCompat.Light" parent=“Base.V21.Theme.AppCompat.Light”></style> res/values-v21/themes_base.xml <style name=“Base.V21.Theme.AppCompat.Light" parent="android:Theme.Material.Light.NoActionBar"> <!-- Update link
colors pre-v23 --> <item name="android:textColorLink">?android:attr/colorAccent</item> … </style> parent="Base.Theme.AppCompat.Light"/> -
<style name="Base.Theme.AppCompat.Light" parent=“Base.V28.Theme.AppCompat.Light”></style> res/values-v28/themes_base.xml <style name=“Base.V28.Theme.AppCompat.Light" parent="Base.V26.Theme.AppCompat.Light"> <!-- We can
use the platform styles on API 28+ --> <item name="dialogCornerRadius">?android:attr/dialogCornerRadius</item> </style> parent="Base.Theme.AppCompat.Light"/> -
• Theme All theme we use is named with `Theme`
prefix. • ThemeOverlay Special themes that overlay the base theme and redefine some of the properties to be applied to a specific views. Theme.AppCompat.Light.DarkActionBar Prefix -
• AppCompat Defined in https://github.com/aosp-mirror/platform_frameworks_support/blob/master/v7/ appcompat/res/values/themes.xml • MaterialComponents Defined in
https://github.com/material-components/material-components-android/blob/ master/lib/java/com/google/android/material/theme/res/values/themes.xml • Design Same as AppCompat. Just alias defined by MaterialComponents. Theme.AppCompat.Light.DarkActionBar Theme (Library) name -
Theme.AppCompat.Light.DarkActionBar • AppCompat • MaterialComponents • (None) • Light •
DayNight • (None) • DarkActionBar • NoActionBar We can find theme we want by combining these parts. -
Theme.MaterialComponents.Light.DarkActionBar.Bridge parent=“Theme.MaterialComponents.Light.DarkActionBar.Bridge”> // parent=“Theme.AppCompat.Light.DarkActionBar”> Bridge themes inherit from AppCompat themes,
but also define the mandatory new Material Components theme attributes to migrate to MaterialComponents easily. https://github.com/material-components/material-components-android/blob/master/docs/getting- started.md#bridge-themes-bridge-themes -
Activity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Set before
setContentView() setTheme(R.style.AppTheme) … } -
Fragment override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? { val contextThemeWrapper = ContextThemeWrapper(activity, R.style.AppTheme) val localInflater = inflater.cloneInContext(contextThemeWrapper) val view = localInflater.inflate( R.layout.fragment_main, container, false ) return view.rootView } -
DayNight theme <style name=“AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar" /> <item name=“android:textColor"> @color/text_color </item>
<item name=“android:colorBackground”> @color/background_color </item> … </style> -
Change mode programmatically val mode = if (isNightMode) { AppCompatDelegate.MODE_NIGHT_YES
} else { AppCompatDelegate.MODE_NIGHT_NO } activity.delegate.setLocalNightMode(mode) // Change mode immediately -
Read code • Android Framework https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/res/values/themes.xml https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/res/values/attrs.xml • AppCompat https://github.com/aosp-mirror/platform_frameworks_support/blob/master/v7/appcompat/res/values/themes_base.xml
https://github.com/aosp-mirror/platform_frameworks_support/blob/master/v7/appcompat/res/values/attrs.xml • MaterialComponents https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/theme/ res/values/themes_base.xml https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/theme/ res/values/attrs.xml -
Read code • Android Framework https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/res/values/themes.xml https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/res/values/attrs.xml • AppCompat https://github.com/aosp-mirror/platform_frameworks_support/blob/master/v7/appcompat/res/values/themes_base.xml
https://github.com/aosp-mirror/platform_frameworks_support/blob/master/v7/appcompat/res/values/attrs.xml • MaterialComponents https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/theme/ res/values/themes_base.xml https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/theme/ res/values/attrs.xml -
• Colors colorPrimary colorSecondary • Drawables selectableItemBackground editTextBackground • Text
appearances textAppearance textAppearanceMedium • Shape appearances shapeAppearanceSmallComponent shapeAppearanceMediumComponent • Widget styles actionBarStyle tabStyle • Themes actionBarTheme dialogTheme • Window configurations windowNoTitle windowActionBar -
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- Colors --> <item name=“colorPrimary”>@color/…</item> <item name=“colorPrimaryVariant”>@color/…</item>
<!-- Text appearances --> <item name=“textAppearanceBody1”>@style/…</item> <item name=“textAppearanceBody2”>@style/..</item> <!-- ShapeAppearances --> <item name=“shapeAppearanceMediumComponent”>@style/…</item> <!-- Widget styles --> <item name=“bottomNavigationStyle”>@style/…</item> <!-- Window configurations—> <item name=“windowTranslucentStatus”>true</item> </style> -
• Colors colorPrimary colorSecondary • Drawables selectableItemBackground editTextBackground • Text
appearances textAppearance textAppearanceMedium • Shape appearances shapeAppearanceSmallComponent shapeAppearanceMediumComponent • Widget styles actionBarStyle tabStyle • Themes actionBarTheme dialogTheme • Window configurations windowNoTitle windowActionBar ① ② ③ -
Attributes reference list • I summarized all the attributes per
each categories to a simple markdown table. https://github.com/konifar/android-theme-attrs-to-markdown-table • This repository would be help you to find the attribute and know each meaning. -
https://material.io/develop/android/theming/color/ AppCompat MaterialComponents Example colorPrimary colorPrimary Toolbar colorPrimaryDark colorPrimaryVariant StatusBar
colorAccent colorSecondary FAB, ProgressBar colorBackground colorBackground Background colorBackgroundFloating colorSurface CardView Differences between AppCompat and MC -
New “colorOn~” for MaterialComponents • colorOnPrimary • colorOnSecondary • colorOnBackground
• colorOnSurface • colorOnError -
Colors appendix • All attributes are here https://github.com/konifar/android-theme-attrs-to-markdown-table/blob/master/outputs/color_attrs.md • Default
values are defined in themes_base.xml Android Platform: https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/res/values/ themes.xml AppCompat: https://github.com/aosp-mirror/platform_frameworks_support/blob/master/v7/appcompat/res/values/ themes_base.xml Material Components: https://github.com/material-components/material-components-android/blob/master/lib/java/ com/google/android/material/theme/res/values/themes_base.xml -
TabStyle <style name="AppTheme" parent=“…”> <item name=“tabStyle">@style/App.Widget.Design.TabLayout</item> <style name=“App.Widget.Design.TabLayout" parent="Widget.Design.TabLayout"> <item
name="elevation">4dp</item> <item name="android:background">?attr/colorPrimary</item> <item name="tabTextColor">?attr/textColorPrimaryInverse</item> </style> -
Step1 : Search theme attribute • Input widget name which
you want to change style. • The naming rule is “xxxStyle” like toolbarStyle, buttonStyle, checkBoxStyle. -
Step2 : Search default style • Input an expected default
style. • The naming rule is “@style/Widget.~” like “Widget.AppCompat.Toolbar” -
Step3 : Search style attributes • I usually search on
GitHub repository. AppCompat: https://github.com/aosp-mirror/platform_frameworks_support/blob/master/appcompat/res/values/styles_base.xml MaterialComponents: https://github.com/material-components/material-components-android/find/master -
Widget styles appendix • All attributes are here https://github.com/konifar/android-theme-attrs-to-markdown-table/blob/master/outputs/widget_style_attrs.md •
Default values are defined in themes_base.xml Android Platform: https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/res/values/ styles.xml AppCompat: https://github.com/aosp-mirror/platform_frameworks_support/blob/master/v7/appcompat/res/values/ styles_base.xml Material Components: Each styles.xml in https://github.com/material-components/material-components-android/tree/ master/lib/java/com/google/android/material -
• shapeAppearanceSmallComponent Buttons, Chips • shapeAppearanceMediumComponent Cards
• shapeAppearanceLargeComponent Bottom Sheets -
cut shape for FAB <!—- themes.xml —-> <item name="floatingActionButtonStyle">@style/Moat.FloatingActionButton</item> <style
name=“Moat.FloatingActionButton” parent="@style/Widget.MaterialComponents.FloatingActionButton"> <item name="shapeAppearanceOverlay">@style/Moat.ShapeAppearance.Fab</item> </style> <style name=“Moat.ShapeAppearance.Fab" parent="ShapeAppearance.MaterialComponents.SmallComponent"> <item name="cornerFamily">cut</item> <item name="cornerSize">@null</item> </style> -
Depends on the design • We can customize almost widget
by using theme. • If the design is common in the app, it should be defined as theme. • If the widget font or shape is specific in the app, it should be defined as style and apply it in layout.xml. -
https://material.io/design/material-studies/fortnightly.html It might be better to define this search edit
text as style, not theme. Chip style should be defined as theme. -
There are many theme attributes, but they are categorized to
only seven types. -
The most important thing is to know how to find
the attributes you want to set. -
The colors and widget styles which should be defined as
theme depends on the design -
Read the material design docs and use the material theming
tools to use theme effectively.