We know that to set margins this is one way of doing it
I discovered this example from this answer. it sets margins in the dp unit type.
val param = xml.layoutParams as ViewGroup.MarginLayoutParams
param.setMargins(left, top, right, bottom)
xml.layoutParams = param
This variable i discovered from this answer. It translates pixels to dp allowing you to then use it for one of the parameters shown in my previous example.
val pxToDP = if ( direction == "left" || direction == "right" ) {
px / ( xml.context.resources.displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)
} else if ( direction == "top" || direction == "bottom" ) {
px / ( xml.context.resources.displayMetrics.ydpi / DisplayMetrics.DENSITY_DEFAULT)
}
But what if i want to set margin using a variable that represent inches? How would i do that?
And are there any other methods i can use to set margin in Kotlin where the variable represents a different unit type? If so, what are the methods?