Development
Learn more by watching the videos after each step
Step 1: Add Window Manager library into the project
Window Manager, a new Jetpack library introduced by Google, helps application developers support new device form factors.
To add the Window Manager library, go to Gradle Scripts > build.gradle (Module: Video_Player.app) and enter the following to the dependencies block:
implementation "androidx.window:window:1.0.0-alpha01"
Step 2: Create a WindowManager instance from the activity
In MainActivity.kt, let’s obtain an instance of Window Manager:
windowManager = WindowManager(this, null)
The default device information is used if you pass null
here. The instance provides display features if you run this on a foldable device, like Galaxy Z Fold2 or Z Flip. Otherwise, an unknown device state is provided, if you run this on a regular phone.
Step 3: Register the DeviceState change listener
To determine changes in the device state (for example, CLOSED
, OPENED
, and HALF_OPENED
), you need to register the DeviceState
change listener:
windowManager.registerDeviceStateChangeCallback(
mainThreadExecutor /* Executor */,
deviceStateChangeCallback /* Consumer<DeviceState> */
)
Step 4: Write the callback function
Now, let’s write a callback function to check the posture change of the device:
inner class DeviceStateChangeCallback : Consumer<DeviceState> {
override fun accept(newDeviceState: DeviceState) {
updateStateAndFeatureViews()
}
}
updateStateAndFeatureViews()
is a user-defined function and not included in the Window Manager library. In this Code Lab activity, it needs to be called inside the callback function to check the posture of the device.
Step 5: Check the current posture of the device
Next, let’s make sure that, in the updateStateAndFeatureViews()
function, the deviceState.posture
is being checked to get the current posture of the device. If you find the posture as POSTURE_HALF_OPENED
, the device is currently in Flex mode. You need to implement your Flex mode UX accordingly and can call for that action from here:
var state= windowManager.deviceState
Step 6: Run the app
After building the APK, you can run the optimized Video Player app on a Galaxy Z Fold2 or Z Flip and see how it adapts when the device is on Flex mode. However, if you don’t have the physical device, you can also test it on a Remote Test Lab device. Watch the video below and know how to easily test your app via Remote Test Lab.
For a better testing experience using Remote Test Lab, choose a device from a location near you.
You're done!
Congratulations! You have successfully achieved the goal of this Code Lab activity. Now, you can implement Flex mode in your app by yourself! But, if you're having trouble, you may check out the link below.
To learn more about developing apps for Galaxy Foldable devices, visit: www.developer.samsung.com/galaxy-z