Is there a way to get the size measurement of the ...
# glance
l
Is there a way to get the size measurement of the current Composable in Glance? Something like
BoxWithConstraints
or a hack with RemoteViews that would expose a measurement.
n
if you set SizeMode.Exact in your GlanceWidget, you can get the size in your composable using LocalSize.current
Copy code
override val sizeMode: SizeMode
    get() = SizeMode.Exact
Copy code
val size = LocalSize.current // Get the available size for the widget

val (widgetWidth, widgetHeight) = remember(size) {
    size.width to size.height
}
Now that i know the widget's total height and width, i calculate scale factor of my design. i pass this scalefactor to my composable which will multiply everything from font to button to padding.
Copy code
val widgetDesignWidth = 140.dp + padding * 2
val widgetDesignHeight = 140.dp + padding * 2
val (scaleFactor, scaleFactorByWidth, scaleFactorByHeight) = calculateScaleFactor(
    widgetDesignWidth,
    widgetDesignHeight,
)
Copy code
@Composable
fun calculateScaleFactor(
    widgetDesignWidth: Dp,
    widgetDesignHeight: Dp,
): Triple<Float, Float, Float> {
    val size = LocalSize.current // Get the available size for the widget
    val widgetWidth = size.width
    val widgetHeight = size.height
    val scaleFactorByWidth = (widgetWidth / widgetDesignWidth)
    val scaleFactorByHeight = (widgetHeight / widgetDesignHeight)
    Log.d(
        "UsageWidgetContentDisplay",
        "widgetDesignWidth: $widgetDesignWidth widgetDesignHeight: $widgetDesignHeight scaleFactorByWidth: $scaleFactorByWidth scaleFactorByHeight: $scaleFactorByHeight",
    )
    return Triple(
        minOf(scaleFactorByWidth, scaleFactorByHeight),
        scaleFactorByWidth,
        scaleFactorByHeight,
    )
}
l
Thanks for your response, I have already found out that using SizeMode.Exact gives the real measurements of the widget as opposed to other SizeModes.
🙌 1
I was wondering whether someone was able to measure the size of the views in Glance on the individual composable level.