@MaterialScopeMarker
public class MaterialScope


Receiver scope which is used by all ProtoLayout Material3 components and layout to support opinionated defaults and to provide the global information for styling Material3 components.

The MaterialScope includes:

  • theme, which is used to retrieve the color, typography or shape values.

  • DeviceParameters, which contains screen size, font scale, renderer schema version etc.

  • Default usage of system theme, with the option to opt out.

Summary

Public methods

final @NonNull DeviceParametersBuilders.DeviceParameters

The device parameters for where the components will be rendered.

Extension functions

final @NonNull LayoutElementBuilders.LayoutElement
ButtonGroupKt.buttonGroup(
    @NonNull MaterialScope receiver,
    @NonNull DimensionBuilders.ContainerDimension width,
    @NonNull DimensionBuilders.ContainerDimension height,
    @Dimension(unit = 0) float spacing,
    @ExtensionFunctionType @NonNull Function1<@NonNull ButtonGroupScopeUnit> content
)

ProtoLayout Material3 component-layout that places its children in a horizontal sequence.

final @NonNull LayoutElementBuilders.LayoutElement

ProtoLayout Material3 component edge button that offers a single slot to take an icon or similar round, small content.

final @NonNull LayoutElementBuilders.LayoutElement

ProtoLayout Material3 component edge button that offers a single slot to take a text or similar long and wide content.

final @NonNull LayoutElementBuilders.LayoutElement
IconKt.icon(
    @NonNull MaterialScope receiver,
    @NonNull String protoLayoutResourceId,
    @NonNull DimensionBuilders.ImageDimension size,
    @NonNull ColorBuilders.ColorProp tintColor
)

Returns the icon components with the defined style.

final @NonNull ColorBuilders.ColorProp
MaterialScopeKt.getColorProp(
    @NonNull MaterialScope receiver,
    int colorToken
)

Retrieves the ColorProp from the customized or default Material theme or dynamic system theme with the given color token name.

final @NonNull ModifiersBuilders.Corner
MaterialScopeKt.getCorner(
    @NonNull MaterialScope receiver,
    int shapeToken
)

Retrieves the Corner shape from the default Material theme with shape token name.

final @NonNull LayoutElementBuilders.LayoutElement

ProtoLayout Material3 full screen layout that represents a suggested Material3 layout style that is responsive and takes care of the elements placement, together with the recommended margin and padding applied.

final @NonNull LayoutElementBuilders.LayoutElement
TextKt.text(
    @NonNull MaterialScope receiver,
    @NonNull TypeBuilders.StringProp text,
    @NonNull TypeBuilders.StringLayoutConstraint stringLayoutConstraint,
    int typography,
    @NonNull ColorBuilders.ColorProp color,
    boolean italic,
    boolean underline,
    boolean scalable,
    int maxLines,
    int multilineAlignment,
    int overflow,
    @NonNull ModifiersBuilders.Modifiers modifiers
)

ProtoLayout component that represents text object holding any information.

Public methods

getDeviceConfiguration

Added in 1.3.0-alpha02
public final @NonNull DeviceParametersBuilders.DeviceParameters getDeviceConfiguration()

The device parameters for where the components will be rendered.

Extension functions

ButtonGroupKt.buttonGroup

public final @NonNull LayoutElementBuilders.LayoutElement ButtonGroupKt.buttonGroup(
    @NonNull MaterialScope receiver,
    @NonNull DimensionBuilders.ContainerDimension width,
    @NonNull DimensionBuilders.ContainerDimension height,
    @Dimension(unit = 0) float spacing,
    @ExtensionFunctionType @NonNull Function1<@NonNull ButtonGroupScopeUnit> content
)

ProtoLayout Material3 component-layout that places its children in a horizontal sequence.

These behave as a group which fill the available space to maximize on screen real estate. The color and size of the child elements should be changed in order to create hierarchy and priority.

The width and height of the Button/Card should be flexible (set to androidx.wear.protolayout.DimensionBuilders.expand or androidx.wear.protolayout.DimensionBuilders.weight) and their proportion should be either equal or weight based. However the buttonGroup displays correctly too if the sizes of elements provided are set to a fixed width/height, although it can lead to more empty space on large screen sizes.

A buttonGroup with more than one row can be created by using multiple buttonGroup and Spacers inside a androidx.wear.protolayout.LayoutElementBuilders.Column:

Column.Builder()
.setWidth(expand())
.setHeight(expand())
.addContent(buttonGroup {...})
.addContent(DEFAULT_SPACER_BETWEEN_BUTTON_GROUPS)
.addContent(buttonGroup {...})
.build()
}

Note that, having more than 2 rows in a Column could lead to too small height of elements that aren't in line with minimum tap target.

Parameters
@NonNull DimensionBuilders.ContainerDimension width

The width of this button group

@NonNull DimensionBuilders.ContainerDimension height

The height of this button group

@Dimension(unit = 0) float spacing

The amount of spacing between buttons

@ExtensionFunctionType @NonNull Function1<@NonNull ButtonGroupScopeUnit> content

The content for each child. The UX guidance is to use no more than 3 elements within a this button group.

EdgeButtonKt.iconEdgeButton

public final @NonNull LayoutElementBuilders.LayoutElement EdgeButtonKt.iconEdgeButton(
    @NonNull MaterialScope receiver,
    @NonNull ModifiersBuilders.Clickable onClick,
    @NonNull TypeBuilders.StringProp contentDescription,
    @NonNull EdgeButtonColors colors,
    @ExtensionFunctionType @NonNull Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> iconContent
)

ProtoLayout Material3 component edge button that offers a single slot to take an icon or similar round, small content.

The edge button is intended to be used at the bottom of a round screen. It has a special shape with its bottom almost follows the screen's curvature. It has fixed height, and takes 1 line of text or a single icon. This button represents the most important action on the screen, and it must occupy the whole horizontal space in its position as well as being anchored to the screen bottom.

This component is not intended to be used with an image background.

import androidx.wear.protolayout.material3.icon
import androidx.wear.protolayout.material3.iconEdgeButton
import androidx.wear.protolayout.material3.materialScope
import androidx.wear.protolayout.material3.text

materialScope(context, deviceConfiguration) {
        iconEdgeButton(onClick = clickable, contentDescription = "Description of a button".prop()) {
            icon("id")
        }
    }
Parameters
@NonNull ModifiersBuilders.Clickable onClick

Associated Clickable for click events. When the button is clicked it will fire the associated action.

@NonNull TypeBuilders.StringProp contentDescription

The content description to be read by Talkback.

@NonNull EdgeButtonColors colors

The colors used for this button. If not set, EdgeButtonDefaults.filled will be used as high emphasis button. Other recommended colors are EdgeButtonDefaults.filledTonal and EdgeButtonDefaults.filledVariant. If using custom colors, it is important to choose a color pair from same role to ensure accessibility with sufficient color contrast.

@ExtensionFunctionType @NonNull Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> iconContent

The icon slot for content displayed in this button. It is recommended to use default styling that is automatically provided by only calling icon with the resource ID.

EdgeButtonKt.textEdgeButton

public final @NonNull LayoutElementBuilders.LayoutElement EdgeButtonKt.textEdgeButton(
    @NonNull MaterialScope receiver,
    @NonNull ModifiersBuilders.Clickable onClick,
    @NonNull TypeBuilders.StringProp contentDescription,
    @NonNull EdgeButtonColors colors,
    @ExtensionFunctionType @NonNull Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> labelContent
)

ProtoLayout Material3 component edge button that offers a single slot to take a text or similar long and wide content.

The edge button is intended to be used at the bottom of a round screen. It has a special shape with its bottom almost follows the screen's curvature. It has fixed height, and takes 1 line of text or a single icon. This button represents the most important action on the screen, and it must occupy the whole horizontal space in its position as well as being anchored to the screen bottom.

This component is not intended to be used with an image background.

import androidx.wear.protolayout.material3.materialScope
import androidx.wear.protolayout.material3.text
import androidx.wear.protolayout.material3.textEdgeButton

materialScope(context, deviceConfiguration) {
        textEdgeButton(onClick = clickable, contentDescription = "Description of a button".prop()) {
            text("Hello".prop())
        }
    }
Parameters
@NonNull ModifiersBuilders.Clickable onClick

Associated Clickable for click events. When the button is clicked it will fire the associated action.

@NonNull TypeBuilders.StringProp contentDescription

The content description to be read by Talkback.

@NonNull EdgeButtonColors colors

The colors used for this button. If not set, EdgeButtonDefaults.filled will be used as high emphasis button. Other recommended colors are EdgeButtonDefaults.filledTonal and EdgeButtonDefaults.filledVariant. If using custom colors, it is important to choose a color pair from same role to ensure accessibility with sufficient color contrast.

@ExtensionFunctionType @NonNull Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> labelContent

The label slot for content displayed in this button. It is recommended to use default styling that is automatically provided by only calling text with the content.

public final @NonNull LayoutElementBuilders.LayoutElement IconKt.icon(
    @NonNull MaterialScope receiver,
    @NonNull String protoLayoutResourceId,
    @NonNull DimensionBuilders.ImageDimension size,
    @NonNull ColorBuilders.ColorProp tintColor
)

Returns the icon components with the defined style.

Material components provide proper defaults for this icon. In order to take advantage of those, this should be used with the resource ID only: icon("id").

Parameters
@NonNull String protoLayoutResourceId

The protolayout resource id of the icon. Node that, this is not an Android resource id.

@NonNull DimensionBuilders.ImageDimension size

The side of an icon that will be used for width and height.

@NonNull ColorBuilders.ColorProp tintColor

The color used to tint the icon.

MaterialScopeKt.getColorProp

public final @NonNull ColorBuilders.ColorProp MaterialScopeKt.getColorProp(
    @NonNull MaterialScope receiver,
    int colorToken
)

Retrieves the ColorProp from the customized or default Material theme or dynamic system theme with the given color token name.

Throws
kotlin.IllegalArgumentException

if the token name is not recognized as one of the constants in ColorTokens

MaterialScopeKt.getCorner

public final @NonNull ModifiersBuilders.Corner MaterialScopeKt.getCorner(
    @NonNull MaterialScope receiver,
    int shapeToken
)

Retrieves the Corner shape from the default Material theme with shape token name.

Throws
kotlin.IllegalArgumentException

if the token name is not recognized as one of the constants in Shape

PrimaryLayoutKt.primaryLayout

public final @NonNull LayoutElementBuilders.LayoutElement PrimaryLayoutKt.primaryLayout(
    @NonNull MaterialScope receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> mainSlot,
    @ExtensionFunctionType Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> titleSlot,
    @ExtensionFunctionType Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> bottomSlot,
    @ExtensionFunctionType Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> labelForBottomSlot,
    ModifiersBuilders.Clickable onClick
)

ProtoLayout Material3 full screen layout that represents a suggested Material3 layout style that is responsive and takes care of the elements placement, together with the recommended margin and padding applied.

This layout is meant to occupy the whole screen, so nothing else should be added on top of it.

On the top, there is an icon that will be automatically placed by the system, followed by the optional title slot. The icon slot needs to be reserved for the whole ProtoLayout Layout and no other content should be added at the top of the screen as it will be overlapped with the system placed icon.

At the bottom, there is an optional fixed slot for either {@link EdgeButton} as a main action or small non tappable content.

The middle of the layout is main content, that will fill the available space. For the best results across different screen sizes, it's recommended that this content's dimension are also DimensionBuilders.expand or DimensionBuilders.weight. Additional content in the main one can be added after a 225dp breakpoint.

import androidx.wear.protolayout.material3.buttonGroup
import androidx.wear.protolayout.material3.icon
import androidx.wear.protolayout.material3.iconEdgeButton
import androidx.wear.protolayout.material3.materialScope
import androidx.wear.protolayout.material3.primaryLayout
import androidx.wear.protolayout.material3.text

materialScope(context, deviceConfiguration) {
        primaryLayout(
            titleSlot = { text("App title".prop()) },
            mainSlot = {
                buttonGroup {
                    // To be populated
                }
            },
            bottomSlot = { iconEdgeButton(clickable, "Description".prop()) { icon("id") } }
        )
    }
Parameters
@ExtensionFunctionType @NonNull Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> mainSlot

The main, central content for this layout. It's recommended for this content to fill the available width and height for the best result across different screen size. This layout places proper padding to prevent content from being cropped by the screen. Note that depending on the corner shapes and different elements on the screen, there might be a need to change padding on some of the elements in this slot. The content passed here can also have an additional content value added to it, after 225dp breakpoint. Some of the examples of content that can be passed in here are: * buttonGroup with buttons or cards * two [buttonGroup * Expanded card

@ExtensionFunctionType Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> titleSlot

The app title in the top slot, just below the icon. This should be one line of text with Typography.TITLE_SMALL typography, describing the main purpose of this layout. Title is an optional slot which can be omitted to make space for other elements. Defaults to ColorTokens.ON_BACKGROUND color.

@ExtensionFunctionType Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> bottomSlot

The content for bottom slot in this layout, that will be anchored to the bottom edge of the screen. This should be either a small non tappable content such as Text with optional label for it or tappable main action with textEdgeButton or iconEdgeButton which is designed to have its bottom following the screen's curvature. This bottom slot is optional, if unset the main content will expand more towards the edge of the screen.

@ExtensionFunctionType Function1<@NonNull MaterialScope, @NonNull LayoutElementBuilders.LayoutElement> labelForBottomSlot

The label displayed just above the bottomSlot. Default will be one line of text with Typography.TITLE_SMALL typography, ColorTokens.ON_SURFACE color that should contain additional description of this layout. When the bottomSlot is not provided or it an edge button, the given label will be ignored.

ModifiersBuilders.Clickable onClick

The clickable action for whole layout. If any area (outside of other added tappable components) is clicked, it will fire the associated action.

public final @NonNull LayoutElementBuilders.LayoutElement TextKt.text(
    @NonNull MaterialScope receiver,
    @NonNull TypeBuilders.StringProp text,
    @NonNull TypeBuilders.StringLayoutConstraint stringLayoutConstraint,
    int typography,
    @NonNull ColorBuilders.ColorProp color,
    boolean italic,
    boolean underline,
    boolean scalable,
    int maxLines,
    int multilineAlignment,
    int overflow,
    @NonNull ModifiersBuilders.Modifiers modifiers
)

ProtoLayout component that represents text object holding any information.

There are pre-defined typography styles that can be obtained from Materials token system in Typography.

import androidx.wear.protolayout.material3.Typography
import androidx.wear.protolayout.material3.materialScope
import androidx.wear.protolayout.material3.text

materialScope(context, deviceConfiguration) {
        text(text = "Hello Material3".prop(), typography = Typography.DISPLAY_LARGE)
    }
import androidx.wear.protolayout.TypeBuilders.StringLayoutConstraint
import androidx.wear.protolayout.TypeBuilders.StringProp
import androidx.wear.protolayout.expression.DynamicBuilders.DynamicString
import androidx.wear.protolayout.material3.ColorTokens
import androidx.wear.protolayout.material3.Typography
import androidx.wear.protolayout.material3.getColorProp
import androidx.wear.protolayout.material3.materialScope
import androidx.wear.protolayout.material3.text

materialScope(context, deviceConfiguration) {
        text(
            text =
                StringProp.Builder("Static")
                    .setDynamicValue(DynamicString.constant("Dynamic"))
                    .build(),
            stringLayoutConstraint = StringLayoutConstraint.Builder("Constraint").build(),
            typography = Typography.DISPLAY_LARGE,
            color = getColorProp(ColorTokens.TERTIARY),
            underline = true,
            maxLines = 5
        )
    }
Parameters
@NonNull TypeBuilders.StringProp text

The text content for this component.

@NonNull TypeBuilders.StringLayoutConstraint stringLayoutConstraint

The layout constraints used to correctly measure Text view size and align text when text has dynamic value.

int typography

The typography from Typography to be applied to this text. This will have predefined default value specified by each components that uses this text, to achieve the recommended look.

@NonNull ColorBuilders.ColorProp color

The color to be applied to this text. It is recommended to use predefined default styles created by each component or getColorProp(token).

boolean italic

Whether text should be displayed as italic.

boolean underline

Whether text should be displayed as underlined.

boolean scalable

Whether text should scale with the user font size.

int maxLines

The maximum number of lines that text can occupy.

int multilineAlignment

The horizontal alignment of the multiple lines of text.

int overflow

The overflow strategy when text doesn't have enough space to be shown.

@NonNull ModifiersBuilders.Modifiers modifiers

The additional Modifiers for this text.