Hello, Good morning everybody. Im curious about 2 ...
# android
s
Hello, Good morning everybody. Im curious about 2 solutions convert 
dp
 to 
px
Solution 1:
Copy code
fun Int.dpToPx(context: Context): Int {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), context.resources.displayMetrics).toInt()
}
Solution 2:
Copy code
fun Int.dpToPx() = (Resources.getSystem().displayMetrics.density * this + 0.5f)
so Which once is correct for multiple screen? Thanks.
l
Solution one
e
aside from the 0.5 adjustment they're the same, but yes, use 1️⃣
no red 2
l
No, they're not the same, the second one doesn't work correctly with configuration changes.
e
ah I thought I read context.resources in that - i instinctively ignored the getSystem
yes, don't use the second solution, period
💯 1
context.resources.displayMetrics would be equivalent but still less clear than the first solution
also… 😶
👍 1
☝🏼 1
s
Thanks @louiscad @ephemient