0

After going through some stack overflow questions, I have written the code to get the height and width in Android for Api level >= 30 and Api level < 30 below :

class MainActivity : AppCompatActivity() {

        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val backgroundThread = Thread {

        val metric: WindowMetrics
        val insets: Insets    // An Insets instance holds four integer offsets which describe changes to the four edges of a Rectangle.
        val displayMetrics: DisplayMetrics
        val width: Int
        val height: Int
        val insetsWidth: Int
        val insetsHeight: Int

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

            // Code for API level 30 and higher

            metric = windowManager.currentWindowMetrics
            insets = windowManager.currentWindowMetrics.windowInsets.getInsetsIgnoringVisibility (WindowInsets.Type.navigationBars() or WindowInsets.Type.displayCutout ())

            insetsWidth = insets.right + insets.left
            insetsHeight = insets.top + insets.bottom

            Log.d ("tally", "${insets.right}, ${insets.left}, ${insets.top}, ${insets.bottom}")

            width = metric.bounds.width () - insetsWidth
            height = metric.bounds.height () - insetsHeight

        } else {

            displayMetrics = DisplayMetrics ()
            windowManager.defaultDisplay.getMetrics (displayMetrics)

            width = displayMetrics.widthPixels
            height = displayMetrics.heightPixels
        }
    }
    backgroundThread.start()
}

/**
 * A native method that is implemented by the 'dpi_poc' native library,
 * which is packaged with this application.
 */
external fun stringFromJNI(): String

companion object {
    // Used to load the 'dpi_poc' library on application startup.
    init {
        System.loadLibrary("dpi_poc")
    }
}

}

I'm not able to understand why I'm still getting the deprecated warning, as shown in the image below, even after enclosing the code in if else block.

enter image description here

What's wrong in the above code and how to solve this warning and write code to get the height and width in android for both api level >= 30 and api level < 30?

7
  • 1
    I don't think the code is wrong. It's just a warning. I think you could annotate the method with @SuppressWarnings("deprecation") to remove it maybe Commented Oct 20, 2023 at 9:56
  • Code is ok. Just tested and it's not warning me with deprecation. Can't reproduce it. Commented Oct 20, 2023 at 9:58
  • Can you post your whole code ? Commented Oct 20, 2023 at 9:59
  • @ItzDavi, have posted the whole code except the import statements. Commented Oct 20, 2023 at 10:07
  • 1
    BTW the compiler will not run your code, that is, it does not matter if there is an if-else checking the version - it will always compile both blocks (the if one and the else one) Commented Oct 20, 2023 at 10:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.