Last active
April 5, 2023 09:24
-
-
Save akshaykalola28/a1e493dac81483ad988387a445b91c86 to your computer and use it in GitHub Desktop.
Extension function for calculate device screen size for android
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.Context | |
import android.graphics.Insets | |
import android.graphics.Rect | |
import android.os.Build | |
import android.util.DisplayMetrics | |
import android.util.Size | |
import android.view.WindowInsets | |
import android.view.WindowManager | |
val Context.screenSize: Size | |
get() { | |
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager | |
val size = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | |
val metrics = windowManager.currentWindowMetrics | |
val windowInsets = metrics.windowInsets | |
val insets: Insets = windowInsets.getInsetsIgnoringVisibility( | |
WindowInsets.Type.navigationBars() | |
or WindowInsets.Type.displayCutout() | |
) | |
val insetsWidth: Int = insets.right + insets.left | |
val insetsHeight: Int = insets.top + insets.bottom | |
val bounds: Rect = metrics.bounds | |
Size( | |
bounds.width() - insetsWidth, | |
bounds.height() - insetsHeight | |
) | |
} else { | |
val displayMetrics = DisplayMetrics() | |
windowManager.defaultDisplay?.getMetrics(displayMetrics) | |
val height = displayMetrics.heightPixels | |
val width = displayMetrics.widthPixels | |
Size(width, height) | |
} | |
return size | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment