-
-
Save e4basil/699b7473457aa6e9c50643025817c479 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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