Skip to content

Instantly share code, notes, and snippets.

@e4basil
Forked from akshaykalola28/DimensionConvert.kt
Last active March 21, 2023 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e4basil/699b7473457aa6e9c50643025817c479 to your computer and use it in GitHub Desktop.
Save e4basil/699b7473457aa6e9c50643025817c479 to your computer and use it in GitHub Desktop.
import android.content.res.Resources
// Convert px to dp
val Int.dp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
//Convert dp to px
val Int.px: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
// getScreenWidth() — gets the screen width in pixels:
fun Context.getScreenWidth(): Int {
val displayMetrics = resources.displayMetrics
return displayMetrics.widthPixels
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment