A new native Android app

After some dabbling on Android… I think I got it.
.toSp() is what I use to convert Dp into TextUnit for the placeholder height and width, in a font-scaling-aware fashion.
On desktop, its implementation is as simple as value / fontScaling.
The Android implementation however… a quick peak will tell you that it’s not that trivial:

Damn, it’s using non-linear font scaling… which applies after any scale higher than 1.03f (i.e. it’s always applied; for reference, my phone has 1.1f font scaling).

This was introduced in a recent Android version (12 or 13 iirc?), and as the name suggests this scales non-linearly depending on the text size (in our case the Dp value).
The aim is to scale small text more and scale bigger text a lot less (so big titles don’t become huge and look broken, as they’re already readable enough).

It also explains why we see the effect outsized in the width but not much in the height.

The fix is simple: do a simple division like Compose Desktop’s toSp():

val placeholder = with(density) {
    Placeholder(
        width = ((placeholderSize.width + 8.dp).value / fontScale).sp,
        height = ((placeholderSize.height + 2.dp).value / fontScale).sp,
        placeholderVerticalAlign = PlaceholderVerticalAlign.Center
    )
}

This works well across any font scale / density.


Collocations:

Made a quick implementation of the HTML parsing for collocations (patterns of use):

It’s a very simple implementation, using the Ksoup library to parse the data into classes. This doesn’t include UI, that should be simple enough.

Full code in Gist.

1 Like