Android dimension with Kotlin: 2.dp 8.sp

Mkhytar Mkhoian
Lalafo
Published in
1 min readNov 26, 2019

--

picture belongs to hipsthetic.com

In Google IO 2019, Google launched Jetpack Compose to create declarative UI. With Compose you can convert dimension to pixels using the extension function. It’s looking like 6.dp . We can use this feature without Jetpack Compose, let’s take a look at how to do that.

Start from writing the extension function.

To be able to use extension function everywhere we need to get display metrics for that, we create AppConfig Kotlin object, save display metrics there, and update when configuration changed.

You need to override onConfigurationChanged method

then override onConfigurationChanged in your Activity

Now we can use those extensions

Thank you for reading!

--

--