0

As Display methods are deprecated in Android 12, I am planning to use Jetpack's backward compatible WindowManager library succeeding Display.

However I am not sure whether I face an issue if I directly access the sizes of a screen in Activity like below:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val windowMetrics = WindowMetricsCalculator.getOrCreate()
        .computeCurrentWindowMetrics(this@WindowMetricsActivity)
    val width = windowMetrics.bounds.width()
    val height = windowMetrics.bounds.height()
}

Because Google's sample code insists using onConfigurationChanged method by using a utility container view like below:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // Add a utility view to the container to hook into
    // View.onConfigurationChanged.
    // This is required for all activities, even those that don't
    // handle configuration changes.
    // We also can't use Activity.onConfigurationChanged, since there
    // are situations where that won't be called when the configuration
    // changes.
    // View.onConfigurationChanged is called in those scenarios.
    // https://issuetracker.google.com/202338815
    container.addView(object : View(this) {
        override fun onConfigurationChanged(newConfig: Configuration?) {
            super.onConfigurationChanged(newConfig)
            logCurrentWindowMetrics("Config.Change")
        }
    })
}
    
@SuppressLint("NotifyDataSetChanged")
private fun logCurrentWindowMetrics(tag: String) {
    val windowMetrics = WindowMetricsCalculator.getOrCreate()
        .computeCurrentWindowMetrics(this@WindowMetricsActivity)
    val width = windowMetrics.bounds.width()
    val height = windowMetrics.bounds.height()
    adapter.append(tag, "width: $width, height: $height")
    runOnUiThread {
        adapter.notifyDataSetChanged()
    }
}

In our project, we directly access the sizes of screens and I am wondering how we migrate to accessing them after onConfigurationChanged invokes and emits the size values by using MAD skills.

Any approaches will be appreciated

pfmaggi
  • 6,116
  • 22
  • 43
Egemen Hamutçu
  • 1,602
  • 3
  • 22
  • 34
  • 1
    Did you see [this article](https://medium.com/androiddevelopers/unbundling-the-stable-windowmanager-a5471ff2907)? – ysfcyln Feb 22 '22 at 13:45
  • please see this answer https://stackoverflow.com/a/70087378/1731626 – Sergio Feb 22 '22 at 14:51
  • 1
    The `WindowMetricsCalculator` is a synchronous API. you can just use that where you need to access the current Window size. The `onConfigurationChanged` snippets is just to get notified when the window size changes (as an example when in multi-window mode the user changes the Window size) – pfmaggi Feb 23 '22 at 07:44

1 Answers1

0

I use the following method to get a screen size starting from Android 11 (API level 30):

fun getScreenSize(context: Context): Size {
     val metrics: WindowMetrics = context.getSystemService(WindowManager::class.java).currentWindowMetrics
     return Size(metrics.bounds.width(), metrics.bounds.height())
}
pfmaggi
  • 6,116
  • 22
  • 43
Sergio
  • 27,326
  • 8
  • 128
  • 149