WindowSizeClass


public final class WindowSizeClass


WindowSizeClass represents breakpoints for a viewport. The recommended width and height break points are presented through windowWidthSizeClass and windowHeightSizeClass. Designers should design around the different combinations of width and height buckets. Developers should use the different buckets to specify the layouts. Ideally apps will work well in each bucket and by extension work well across multiple devices. If two devices are in similar buckets they should behave similarly.

This class is meant to be a common definition that can be shared across different device types. Application developers can use WindowSizeClass to have standard window buckets and design the UI around those buckets. Library developers can use these buckets to create different UI with respect to each bucket. This will help with consistency across multiple device types.

A library developer use-case can be creating some navigation UI library. For a size class with the WindowWidthSizeClass.EXPANDED width it might be more reasonable to have a side navigation. For a WindowWidthSizeClass.COMPACT width, a bottom navigation might be a better fit.

An application use-case can be applied for apps that use a list-detail pattern. The app can use the WindowWidthSizeClass.MEDIUM to determine if there is enough space to show the list and the detail side by side. If all apps follow this guidance then it will present a very consistent user experience.

In some cases developers or UI systems may decide to create their own break points. A developer might optimize for a window that is smaller than the supported break points or larger. A UI system might find that some break points are better suited than the recommended break points. In these cases developers may wish to specify their own custom break points and match using a when statement.

Summary

Public constructors

WindowSizeClass(int widthDp, int heightDp)

the primary constructor taking the bounds of the size class.

Public methods

static final @NonNull WindowSizeClass
compute(float dpWidth, float dpHeight)

Computes the recommended WindowSizeClass for the given width and height in DP.

static final @NonNull WindowSizeClass
@ExperimentalWindowCoreApi
compute(int widthPx, int heightPx, float density)

Computes the WindowSizeClass for the given width and height in pixels with density.

boolean
equals(Object other)
final int

the height in DP for the size class.

final int

the width in DP for the size class.

final @NonNull WindowHeightSizeClass

Returns the WindowHeightSizeClass that corresponds to the heightDp.

final @NonNull WindowWidthSizeClass

Returns the WindowWidthSizeClass that corresponds to the widthDp.

int
@NonNull String

Extension functions

final int
@ExperimentalWindowCoreApi
WindowSizeClassScoreCalculatorKt.scoreWithinAreaBounds(
    @NonNull WindowSizeClass receiver,
    int windowWidthDp,
    int windowHeightDp
)

A scoring function to calculate how close the area of a WindowSizeClass is to the area of a window without exceeding it.

final int

A scoring function to calculate how close the height of a WindowSizeClass is to heightDp without exceeding it.

final int

A scoring function to calculate how close the width of a WindowSizeClass is to widthDp without exceeding it.

Public constructors

WindowSizeClass

Added in 1.3.0-beta01
public WindowSizeClass(int widthDp, int heightDp)

the primary constructor taking the bounds of the size class.

Public methods

compute

Added in 1.1.0
public static final @NonNull WindowSizeClass compute(float dpWidth, float dpHeight)

Computes the recommended WindowSizeClass for the given width and height in DP.

Parameters
float dpWidth

width of a window in DP.

float dpHeight

height of a window in DP.

Returns
@NonNull WindowSizeClass

WindowSizeClass that is recommended for the given dimensions.

See also
widestOrEqualWidthDp

for selecting from a custom set of WindowSizeClass.

compute

Added in 1.3.0-beta01
@ExperimentalWindowCoreApi
public static final @NonNull WindowSizeClass compute(int widthPx, int heightPx, float density)

Computes the WindowSizeClass for the given width and height in pixels with density.

Parameters
int widthPx

width of a window in PX.

int heightPx

height of a window in PX.

float density

density of the display where the window is shown.

Returns
@NonNull WindowSizeClass

WindowSizeClass that is recommended for the given dimensions.

equals

public boolean equals(Object other)

getHeightDp

Added in 1.3.0-beta01
public final int getHeightDp()

the height in DP for the size class.

getWidthDp

Added in 1.3.0-beta01
public final int getWidthDp()

the width in DP for the size class.

getWindowHeightSizeClass

Added in 1.1.0
public final @NonNull WindowHeightSizeClass getWindowHeightSizeClass()

Returns the WindowHeightSizeClass that corresponds to the heightDp.

getWindowWidthSizeClass

Added in 1.1.0
public final @NonNull WindowWidthSizeClass getWindowWidthSizeClass()

Returns the WindowWidthSizeClass that corresponds to the widthDp.

hashCode

public int hashCode()

toString

public @NonNull String toString()

Extension functions

WindowSizeClassScoreCalculatorKt.scoreWithinAreaBounds

@ExperimentalWindowCoreApi
public final int WindowSizeClassScoreCalculatorKt.scoreWithinAreaBounds(
    @NonNull WindowSizeClass receiver,
    int windowWidthDp,
    int windowHeightDp
)

A scoring function to calculate how close the area of a WindowSizeClass is to the area of a window without exceeding it.

Parameters
int windowWidthDp

the width of a window constraint.

int windowHeightDp

the height of a window constraint.

Returns
int

an integer from -1 to Integer.MAX_VALUE where a larger value indicates a better match.

WindowSizeClassScoreCalculatorKt.scoreWithinHeightDp

@ExperimentalWindowCoreApi
public final int WindowSizeClassScoreCalculatorKt.scoreWithinHeightDp(
    @NonNull WindowSizeClass receiver,
    int heightDp
)

A scoring function to calculate how close the height of a WindowSizeClass is to heightDp without exceeding it.

Parameters
int heightDp

the height bound to try to match.

Returns
int

an integer from -1 to Integer.MAX_VALUE where a larger value indicates a better match.

WindowSizeClassScoreCalculatorKt.scoreWithinWidthDp

@ExperimentalWindowCoreApi
public final int WindowSizeClassScoreCalculatorKt.scoreWithinWidthDp(
    @NonNull WindowSizeClass receiver,
    int widthDp
)

A scoring function to calculate how close the width of a WindowSizeClass is to widthDp without exceeding it.

Parameters
int widthDp

the width bound to try to match.

Returns
int

an integer from -1 to Integer.MAX_VALUE where a larger value indicates a better match.