• Kotlin Version
via Extension Property
If you want to know the size of the screen in pixels as well as dp
, using these extension properties really helps:
import android.content.Context
import android.content.res.Resources
import android.graphics.Rect
import android.graphics.RectF
import android.os.Build
import android.util.DisplayMetrics
import android.view.WindowManager
import kotlin.math.roundToInt
/**
* @author aminography
*/
private val displayMetrics: DisplayMetrics by lazy { Resources.getSystem().displayMetrics }
/**
* Returns boundary of the screen in pixels (px).
*/
val screenRectPx: Rect
get() = displayMetrics.run { Rect(0, 0, widthPixels, heightPixels) }
/**
* Returns boundary of the screen in density independent pixels (dp).
*/
val screenRectDp: RectF
get() = screenRectPx.run { RectF(0f, 0f, right.px2dp, bottom.px2dp) }
/**
* Returns boundary of the physical screen including system decor elements (if any) like navigation
* bar in pixels (px).
*/
val Context.physicalScreenRectPx: Rect
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
(applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager)
.run { DisplayMetrics().also { defaultDisplay.getRealMetrics(it) } }
.run { Rect(0, 0, widthPixels, heightPixels) }
} else screenRectPx
/**
* Returns boundary of the physical screen including system decor elements (if any) like navigation
* bar in density independent pixels (dp).
*/
val Context.physicalScreenRectDp: RectF
get() = physicalScreenRectPx.run { RectF(0f, 0f, right.px2dp, bottom.px2dp) }
/**
* Converts any given number from pixels (px) into density independent pixels (dp).
*/
val Number.px2dp: Float
get() = this.toFloat() / displayMetrics.density
/**
* Converts any given number from density independent pixels (dp) into pixels (px).
*/
val Number.dp2px: Int
get() = (this.toFloat() * displayMetrics.density).roundToInt()
Usage:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val widthPx = screenRectPx.width()
val heightPx = screenRectPx.height()
println("[PX] screen width: $widthPx , height: $heightPx")
val widthDp = screenRectDp.width()
val heightDp = screenRectDp.height()
println("[DP] screen width: $widthDp , height: $heightDp")
println()
val physicalWidthPx = physicalScreenRectPx.width()
val physicalHeightPx = physicalScreenRectPx.height()
println("[PX] physical screen width: $physicalWidthPx , height: $physicalHeightPx")
val physicalWidthDp = physicalScreenRectDp.width()
val physicalHeightDp = physicalScreenRectDp.height()
println("[DP] physical screen width: $physicalWidthDp , height: $physicalHeightDp")
}
}
Result:
When the device is in portrait
orientation:
[PX] screen width: 1440 , height: 2392
[DP] screen width: 360.0 , height: 598.0
[PX] physical screen width: 1440 , height: 2560
[DP] physical screen width: 360.0 , height: 640.0
When the device is in landscape
orientation:
[PX] screen width: 2392 , height: 1440
[DP] screen width: 598.0 , height: 360.0
[PX] physical screen width: 2560 , height: 1440
[DP] physical screen width: 640.0 , height: 360.0