Tuesday, September 12, 2023
HomeMobileJetpack WindowManager 1.1 is steady!

Jetpack WindowManager 1.1 is steady!



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 screen area. Many new gadgets have entered the market, and plenty of new use instances have been unlocked!

Jetpack WindowManager is without doubt one of the most essential libraries for optimizing your Android app for various type elements. And this launch is a serious milestone that features numerous new options and enhancements.

Let’s recap all of the use instances coated by the Jetpack WindowManager library.

Get window metrics (and dimension courses!)

Traditionally, builders relied on the system show dimension to resolve the structure of their apps, however with the supply of various type elements (akin to foldables) and show modes (akin to multi-window and multi-display) details about the scale of the app window relatively than the system show has turn out to be important.

The Jetpack WindowManager WindowMetricsCalculator interface gives the supply of fact to measure how a lot display screen area is presently accessible on your app.

Constructed on high of that, the window dimension courses are a set of opinionated viewport breakpoints that allow you to design, develop, and check responsive and adaptive utility layouts. The breakpoints have been chosen particularly to steadiness structure simplicity with the pliability to optimize your app for distinctive instances.

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

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

non-public 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.peak() /
assets.displayMetrics.density
val heightWindowSizeClass = when {
heightDp < 480f -> WindowSizeClass.COMPACT
heightDp < 900f -> WindowSizeClass.MEDIUM
else -> WindowSizeClass.EXPANDED
}
}

To be taught extra, see our Assist totally different display screen sizes developer information.

Make your app fold conscious

Jetpack WindowManager additionally gives all of the APIs it’s worthwhile to optimize the structure for foldable gadgets.

Particularly, use WindowInfoTracker to question FoldingFeature info, akin to:

  • state: The folded state of the system, FLAT or HALF_OPENED
  • orientation: The orientation of the fold or system 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 function throughout the utility window (inherited from DisplayFeature)

You may entry this information by means of a Circulate:

override enjoyable onCreate(savedInstanceState: Bundle?) {
...
lifecycleScope.launch(Dispatchers.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 utilize the info to create an optimized structure for the present system state, for instance, by implementing tabletop mode! You may see a tabletop mode instance in MediaPlayerActivity.kt.

An amazing place to start out studying about foldables is our codelab: Assist foldable and dual-screen gadgets with Jetpack WindowManager.

Present two Actions aspect by aspect

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

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

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

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

It is usually the largest 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 conduct of the break up display screen (break up ratio, guidelines, ending conduct)
  • Outline placeholders
  • Test (and alter) the break up state at runtime
  • Implement horizontal splits
  • Begin a modal in full window

Concerned about exploring exercise embedding? We’ve obtained you coated with a devoted codelab: Construct a list-detail structure with exercise 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 permits visible integration of actions from a number of Android functions. The system shows an exercise of the host app and an embedded exercise from one other app on display screen aspect by aspect or high and backside, simply as in single-app exercise embedding.

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

You may be taught extra about cross-application embedding within the Exercise embedding developer information.

Conclusion

Jetpack WindowManager is without doubt one of the most essential libraries you must be taught if you wish to optimize your app’s person expertise for various type elements.

WindowManager can also be including new, attention-grabbing options with each launch, so preserve a watch out for what’s coming in model 1.2.

See the Jetpack WindowManager documentation and pattern app to get began with WindowManager at the moment!



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments