Skip to content

Instantly share code, notes, and snippets.

@sudeshim3
Created October 5, 2019 11:50
Show Gist options
  • Save sudeshim3/d27b39c523d0e88f3fe037973292973e to your computer and use it in GitHub Desktop.
Save sudeshim3/d27b39c523d0e88f3fe037973292973e to your computer and use it in GitHub Desktop.
//UI Unit conversion.
fun Context.pxToDp(dimension: Int) = dimension / this.resources.displayMetrics.density
fun Context.pxToSp(dimension: Int) = dimension / this.resources.displayMetrics.scaledDensity
fun Context.dpToPx(dimension: Int) = dimension * this.resources.displayMetrics.density
fun Context.spToPx(dimension: Int) = dimension * this.resources.displayMetrics.scaledDensity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment