How to get size in pixel on device dynamically in code?
Screen
To get screen height:
1 2 3 4 5 6 7 8
/** * Get screen height in pixel * @param context Context used to calculate screen size * @return Screen height in pixel */ funscreenHeightInPx(context: Context): Int { return context.resources.displayMetrics.heightPixels }
Status Bar
To get status bar height:
1 2 3 4 5 6 7 8 9
/** * Get status bar height in pixel * @param context Context used to calculate status bar size * @return Status bar size in pixel */ funstatusBarHeightInPx(context: Context): Int { val resourceIdStatusBar = context.resources.getIdentifier("status_bar_height", "dimen", "android") returnif (resourceIdStatusBar > 0) context.resources.getDimensionPixelSize(resourceIdStatusBar) else0 }
Tab Bar (Bottom Navigation)
This is the Tab Bar (in iOS). Bottom Navigation in Android. To get its height:
1 2 3 4 5 6 7 8 9
/** * Get navigation bar height in pixel * @param context Context used to calculate tab bar size * @return Navigation bar size in pixel */ funtabBarHeightInPx(context: Context): Int { val resourceIdTabBar = context.resources.getIdentifier("navigation_bar_height", "dimen", "android") returnif (resourceIdTabBar > 0) context.resources.getDimensionPixelSize(resourceIdTabBar) else0 }