안드로이드
안드로이드 코틀린 화면 사이즈, StatusBar, ActionBar, NavigationBar 확인하기.
캬옹냐옹
2021. 1. 7. 10:43
728x90
stackoverflow.com/questions/4743116/get-screen-width-and-height-in-android
Get screen width and height in Android
How can I get the screen width and height and use this value in: @Override protected void onMeasure(int widthSpecId, int heightSpecId) { Log.e(TAG, "onMeasure" + widthSpecId);
stackoverflow.com
여기에 방법이 나와있긴 하지만 API 30 이상부터는 지원 되지 않는다고 한다.
동작은 되지만 코드에 줄가있는게 보기 싫어서 다른 방법을 찾아봤다.
아래 코드들이다.
화면 사이즈 구하는 코드
val heightPixels = resources.displayMetrics.heightPixels
val widthPixels = resources.displayMetrics.widthPixels
val xdpi = resources.displayMetrics.xdpi
val ydpi = resources.displayMetrics.ydpi
Log.d(LOG, "heightPixels:${heightPixels}")
Log.d(LOG, "widthPixels:${widthPixels}")
Log.d(LOG, "xdpi:${xdpi}")
Log.d(LOG, "ydpi:${ydpi}")
StatusBar 구하는 코드
var statusBarHeight = 0
val resId = resources.getIdentifier("status_bar_height", "dimen", "android")
if (resId > 0) {
statusBarHeight = resources.getDimensionPixelSize(resId)
}
ActionBar 구하는 코드
var actionBarHeight = 0
val styledAttributes = theme.obtainStyledAttributes(intArrayOf(android.R.attr.actionBarSize))
actionBarHeight = styledAttributes.getDimension(0, 0f).toInt()
styledAttributes.recycle()
NavigationBar 구하는 코드
val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
var deviceHeight = 0
if (resourceId > 0) {
deviceHeight = resources.getDimensionPixelSize(resourceId)
}
728x90