8

Material Theming with MDC: Type

 3 years ago
source link: https://medium.com/androiddevelopers/material-theming-with-mdc-type-8c2013430247
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Image for post
Image for post

Material Theming with MDC: Type

Type theming on Android using the MDC library

This article is also posted on the Material Design blog.

Material Theming is a way to customize Material Components to align with your brand. A Material theme includes color, typography and shape parameters which you can adjust to get near-infinite variations of the components — all while maintaining their core anatomy and usability.

Image for post
Image for post

On Android, Material Theming can be implemented using the Material Components (MDC) library, from version 1.1.0 onwards. If you’re looking to migrate from the Design Support Library or MDC 1.0.0, take a look at our migration guide.

This article will be focusing on type theming.

Type attributes

Material Design provides 13 type “styles” that are applied to all the text in your app. Each of these have a design term (eg. “Body 1”) along with a corresponding type attribute that can be overridden in your app theme (eg. textAppearanceBody1). There are default “baseline” values (text size, letter spacing, capitalization, etc.) for each style.

Image for post
Image for post
MDC type attributes with baseline styling

Material Components use these type attributes to style textual elements of the widgets (those parts that subclass or comprise one or more TextViews).

Image for post
Image for post
Type attributes used by a button

They are applied with eg.

android:textAppearance=”?attr/textAppearanceBody1”

in layouts and widget styles. Take a look at Nick Butcher’s “What’s your text’s appearance?” article for more information on using this and the order of precedence with other styling techniques.

In the MDC themes these attributes map to styles, eg.

<style name=”Theme.MaterialComponents.*” parent="...">
...
<item name=”textAppearanceBody1”>
@style/TextAppearance.MaterialComponents.Body1
</item>
<style />

You might recognize TextAppearance styles from AppCompat or the platform and these are discussed in more detail in the Type resources section below. The corresponding attributes are new to MDC and give you the ability to vary typography by theme.

Picking type

Figuring out which type styles to use and the values within them may be the responsibility of a designer, or derived from your product’s brand. However, it’s still useful to know about the role of each style and in which scenarios they should be used:

  • textAppearanceHeadline* styles are for headings
  • textAppearanceSubtitle* styles are for subheadings
  • textAppearanceBody* styles are for multiline body text
  • textAppearanceButton is for buttons but also maps to parts of other components like tabs and dialog actions
  • textAppearanceCaption is for smaller text like text field hints and errors
  • textAppearanceOverline is also for smaller text but capitalization and increased letter spacing make it suitable for small titles and labels like date picker headers

Type tool

Material Design provides a useful tool for previewing type scales, integrating with Google Fonts and exporting code. See “Type scale generator” in the Material Design type scale guidelines.

Image for post
Image for post
Google Fonts (left) and Type scale generator (right)

Type resources

Type resources consist of fonts as well as TextAppearance styles. Let’s take a look at what’s available on Android and a few things to keep in mind when declaring styles.

XML and downloadable fonts

Fonts are stored in the res/font directory and referenced with @font/ notation. You can use either local XML fonts or downloadable fonts. Android Studio offers a built-in wizard to get you started with downloadable fonts including the necessary certificates and manifest metadata. You may also want to check out “The Android Developer’s Guide to Better Typography” by Rod Sheeter for a more detailed guide and further optimizations to font preloading.

In general we recommend using downloadable fonts as they reduce the size of your app bundle by leveraging the shared font provider cache. However, downloadable fonts currently only work with those available on Google Fonts. If your app requires a purchased or proprietary font, use XML fonts.

It’s also worth mentioning that variable fonts are supported on Android from API 26 onwards. Take a look at Rebecca Franks’ “Variable Fonts in Android O 🖍” article for more information.

TextAppearance styles

TextAppearance styles can be seen as the Android equivalent of Material Design type styles. For custom styles, we recommend two approaches to help separate concerns and create a single source of truth for type theming values in your app:

  • Store all TextAppearance styles in a single res/values/type.xml file
  • Use the MDC TextAppearance styles as parents and adhere to the same naming convention

Attributes and values you can use within these styles align with those supported by TextView:

  • fontFamily is the font family, typically an @font/ resource referring to an XML or downloadable font
  • android:textSize is the size of the text, typically an sp dimension
  • android:textColor is the color of text
  • android:letterSpacing is the spacing between characters
  • android:textAllCaps is a boolean to toggle text capitalization
  • android:textFontWeight is the weight of the font, used to select the closest match from a font family, but is only available on API 28 and above. android:textStyle can also be used to apply transformations such as bold and italic.

Calculating letterSpacing

Letter spacing values on Android tend to use a different unit of measurement (em) to those used in design tools like Sketch (tracking). The Material Design typography guidelines provide a relatively simple equation for determining suitable em values from tracking values:

(Tracking from Sketch / font size in sp) = letter spacing

MaterialTextView and lineHeight

The platform TextView added support for the android:lineHeight attribute in API 28. MDC provides backported support for this via the MaterialTextView class. You don’t need to use this class directly in layouts as <TextView>s will get auto-inflated as MaterialTextViews by MaterialComponentsViewInflater.

You can use lineHeight in various scenarios:

  • Included as an item in TextAppearance styles (applied with android:textAppearance="...")
  • Included as an item in widget styles with parent Widget.MaterialComponents.TextView (applied with style="...")
  • Applied directly to <TextView>s in layouts

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK