Apps

Jetpack WindowManager 1.1 is steady!

[ad_1]


Posted by Francesco Romano, Developer Relations Engineer on Android

It’s been greater than a yr for the reason that launch of the Jetpack WindowManager 1.0 steady model, and plenty of issues have occurred within the foldables and huge display house. Many new units have entered the market, and plenty of new use circumstances have been unlocked!

Jetpack WindowManager is among the most vital libraries for optimizing your Android app for various kind components. And this launch is a significant milestone that features numerous new options and enhancements.

Let’s recap all of the use circumstances lined by the Jetpack WindowManager library.

Get window metrics (and measurement courses!)

Traditionally, builders relied on the machine show measurement to determine the structure of their apps, however with the provision of various kind components (corresponding to foldables) and show modes (corresponding to multi-window and multi-display) details about the dimensions of the app window slightly than the machine show has develop into important.

The Jetpack WindowManager WindowMetricsCalculator interface supplies the supply of reality to measure how a lot display house is at the moment out there in your app.

Constructed on prime of that, the window size classes are a set of opinionated viewport breakpoints that show you how to design, develop, and check responsive and adaptive utility layouts. The breakpoints have been chosen particularly to steadiness structure simplicity with the flexibleness to optimize your app for distinctive circumstances.

With Jetpack Compose, use window measurement courses by importing them from the androidx.compose.material3 library, which makes use of WindowMetricsCalculator internally.

For View-based app, you should use the next code snippet to compute the window measurement courses:

personal enjoyable computeWindowSizeClasses() {
val metrics = WindowMetricsCalculator.getOrCreate()
.computeCurrentWindowMetrics(this)

val widthDp = metrics.bounds.width() /
assets.displayMetrics.density
val widthWindowSizeClass = when {
widthDp < 600f -> WindowSizeClass.COMPACT
widthDp < 840f -> WindowSizeClass.MEDIUM
else -> WindowSizeClass.EXPANDED
}

val heightDp = metrics.bounds.top() /
assets.displayMetrics.density
val heightWindowSizeClass = when {
heightDp < 480f -> WindowSizeClass.COMPACT
heightDp < 900f -> WindowSizeClass.MEDIUM
else -> WindowSizeClass.EXPANDED
}
}

To study extra, see our Support different screen sizes developer information.

Make your app fold conscious

Jetpack WindowManager additionally supplies all of the APIs you could optimize the structure for foldable units.

Specifically, use WindowInfoTracker to question FoldingFeature info, corresponding to:

  • state: The folded state of the machine, FLAT or HALF_OPENED
  • orientation: The orientation of the fold or machine hinge, HORIZONTAL or VERTICAL
  • occlusion kind: Whether or not the fold or hinge conceals a part of the show, NONE or FULL
  • is separating: Whether or not the fold or hinge creates two logical show areas, true or false
  • bounds: The bounding rectangle of the characteristic throughout the utility window (inherited from DisplayFeature)

You’ll be able to entry this information by way of a Flow:

override enjoyable onCreate(savedInstanceState: Bundle?) {
...
lifecycleScope.launch(Dispatchers.Most important) {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
WindowInfoTracker.getOrCreate(this@MainActivity)
.windowLayoutInfo(this@MainActivity)
.accumulate { layoutInfo ->

val foldingFeature = layoutInfo.displayFeatures

}
}
}
}

When you accumulate the FoldingFeature data, you should use the info to create an optimized structure for the present machine state, for instance, by implementing tabletop mode! You’ll be able to see a tabletop mode instance in MediaPlayerActivity.kt.

A fantastic place to begin studying about foldables is our codelab: Support foldable and dual-screen devices with Jetpack WindowManager.

Present two Actions facet by facet

Final, however not least, you should use the most recent steady Jetpack WindowManager API: activity embedding.

Obtainable since Android 12L, exercise embedding allows builders with legacy multi-activiity architectures to show a number of actions from the identical utility—and even from a number of purposes—side-by-side on giant screens.

It’s a good way to implement list-detail layouts with minimal or no code modifications.

Observe: Trendy Android Improvement (MAD) recommends utilizing a single-activity structure primarily based on Jetpack APIs, together with Jetpack Compose. In case your app makes use of fragments, take a look at SlidingPaneLayout. Exercise embedding is designed for multiple-activity, legacy apps that may’t be simply up to date to MAD.

Additionally it is the most important change within the library, because the exercise embedding APIs at the moment are steady in 1.1!

Not solely that, however the API is now richer in options, because it allows you to:

  • Modify the habits of the break up display (break up ratio, guidelines, ending habits)
  • Outline placeholders
  • Examine (and alter) the break up state at runtime
  • Implement horizontal splits
  • Begin a modal in full window

Considering exploring exercise embedding? We’ve received you lined with a devoted codelab: Build a list-detail layout with activity embedding.

Many apps are already utilizing exercise embedding in manufacturing, for instance, WhatsApp:

Image of WhatsApp on a large screen device showing activity embedding

And ebay!

Image of Ebay on a large screen device showing activity embedding

Implementing list-details layouts with a number of actions is just not the one use case of exercise embedding!

Ranging from Android 13 (API degree 33), apps can embed actions from different apps.

Cross‑utility exercise embedding allows visible integration of actions from a number of Android purposes. The system shows an exercise of the host app and an embedded exercise from one other app on display facet by facet or prime and backside, simply as in single-app exercise embedding.

Host apps implement cross-app exercise embedding the identical approach they implement single-app exercise embedding, however the embedded app should opt-in for safety causes.

You’ll be able to study extra about cross-application embedding within the Activity embedding developer information.

Conclusion

Jetpack WindowManager is among the most vital libraries it’s best to study if you wish to optimize your app’s person expertise for various kind components.

WindowManager can be including new, attention-grabbing options with each launch, so hold an eye fixed out for what’s coming in model 1.2.

See the Jetpack WindowManager documentation and sample app to get began with WindowManager as we speak!

[ad_2]

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button