How to use point in dp programmatically in Android

Issue #382

1
2
3
4
5
6
7
8
9
10
import android.content.Context

fun Int.toDp(context: Context?): Int {
if (context != null) {
val scale = context.resources.displayMetrics.density
return (this.toFloat() * scale + 0.5f).toInt()
} else {
return 0
}
}
1
2
val set = ConstraintSet()
set.setMargin(imageView.id, ConstraintSet.RIGHT, rightMargin.toDp(150))

Read more

Comments