With the launch of the Samsung Galaxy Fold and Huawei Mate X, foldable phones are starting to take off. While changing the mobile experience, it also brings more challenges for us developers in terms of adaptation. This article introduces some concepts related to Android development and folding screen, and how to adapt the folding screen.

Folding screen adaptation

Folding screens need to be adapted because the screen size of our application may change as it runs, which can cause some problems for existing projects.

Therefore, the essence of foldable screen adaptation is: when the application is running, the size, density or proportion of the screen changes, and the application can continue to display and run normally on the changed screen.

In fact, this situation is not only after the introduction of a collapsed screen, the same thing happens to the application landscape switch, but many applications force portrait, do not need to deal with this adaptation.

Allows changing the application size

To adapt to a foldable screen, the first step is to enable the Application to dynamically change its size. We need to declare under the Application or corresponding Activity in Menifest:

android:resizeableActivity="true"
Copy the code

On the contrary, if you are not going to adapt for the time being, set this parameter to false.

Note that this parameter defaults to true on Android 7.0 or later, and to false below.

Here are two concepts related to this parameter.

Split screen mode

Starting with Android 7.0, the resizeableActivity defaults to True because of a new feature added in 7.0 called split screen mode.

Setting resizeableActivity to False indicates that the application does not support split screen mode, which determines whether the application has a split screen option.

Compatibility mode

When resizeableActivity is set to false, expanding the screen might look something like this:

The effect is similar to using an incompatible iPhone app on the iPad, with a black surround called compatibility mode.

The display of compatible mode is related to the maximum supported ratio maxAspectRatio. When the screen ratio exceeds maxAspectRatio, the screen will be filled with black edges. It is recommended that maxAspectRatio be set to 2.4 (12: 5) Modify maxAspectRatio as follows:

  • Android 8.0 or later

Configure android:maxAspectRatio:

<activity android:name=".MainActivity"
          android:maxAspectRatio="2.4" />
Copy the code
  • Android 8.0 or later

Add meta-data named Android.max_aspect to the tag:

<meta-data android:name="android.max_aspect" android:value="2.4" />
Copy the code

If resizeableActivity is set to True, maxAspectRatio does not need to be set and will not take effect.

Monitor size change

By default, when the screen changes, the system destroys and recreates the entire Activity. But we want the screen to change so that the program can continue running as it was before the switch, without the need to restart the page.

We can add a configuration to the Activity:

android:configChanges="screenSize|smallestScreenSize|screenLayout"
Copy the code

After this configuration, the Activity is not restarted when the screen changes. Instead, the onConfigurationChanged method is called. We can retrieve the current screen information from this method:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Log.i("config", "newConfig.screenHeightDp:" + newConfig.screenHeightDp
            + ", newConfig.screenWidthDp" + newConfig.screenWidthDp);
}
Copy the code

After this change, you need to pay attention to testing to see if the layout of the page is distorted, and if it is not, you need to modify the layout to accommodate different resolutions.

We can also update the layout according to the screen information, such as switching the LinearLayout to GridLayout on the big screen to make full use of the display space of the big screen, which is a further optimization:

Android Q

In the upcoming Android Q, a number of features have been added to support folding screens.

Multi-resume

As for the split screen mode, the old split screen only supported two apps at the same time, but the larger screen opens up more possibilities and now allows more than two apps to be displayed at the same time.

Prior to Android Q, in apps running in split-screen mode, only activities that get focus are in the onResume state, and all other visible activities are in the onPause state.

On Android Q, all top-level visible activities are in the onResume state, ensuring that visible activities run properly in split screen mode. But there is still only one Activity that can get focus, and we call this Activity TopResumedActivity.

In Android Q Activity added a lifecycle callback method onTopResumedActivityChanged (), it will be called when the Activity to obtain or loses focus, can be used to judge whether the current Activity has focus:

Protected void onTopResumedActivityChanged (Boolean topResumed) {if (topResumed) {/ / access to focus} else {/ / lost focus}}Copy the code

This method is used when we use an exclusive resource. What is monopolizing resources? Microphones and cameras are examples of resources that can only be used by one Activity at a time.

Multiple Activity such as split screen mode is used by the camera, but this time only access to the focus of the Activity have access, this case will be through onTopResumedActivityChanged to focus () to judge whether the current Activity. You can not release the camera when you lose focus, but you need to handle the disconnection and reconnection of the camera.

minAspectRatio

Before the Android Q only configuration support proportion biggest maxAspectRatio, Android Q can configure the minimum support proportion minAspectRatio now, usage and maxAspectRatio:

<activity android:name=".MainActivity"
          android:maxAspectRatio="2.4"
          android:minAspectRatio="1"/>
Copy the code

The maximum and minimum supported ratios are only useful when resizeableActivity is set to false.

debugging

Of course, the best debugging tool is to use the real machine, but only a few people have this condition, the following are two debugging programs outside the real machine.

Android Studio

In Android Studio 3.5, we added a simulator for foldable devices. We can create one to debug:

By clicking the button on the emulator, we can toggle the collapse and expansion states of the virtual machine:

ADB

We can dynamically modify the resolution of the phone through the command line to achieve the effect of simulating the folding screen switch. Take the resolution of Mate X as an example, we first use the command line:

adb shell wm size 1148x2480
Copy the code

The phone’s resolution will simulate 1148×2480, which is the Mate X’s resolution when folded, and then type:

adb shell wm size 2200x2480
Copy the code

The phone’s resolution was changed to 2200×2480 when the Mate X was expanded, which simulated the switch when the foldable screen was expanded.

You can change the resolution again to 1148×2480 to simulate the screen folding switch. Finally, use the following command line to restore the phone’s own resolution:

adb shell wm size reset
Copy the code

The end of the

So much for folding screen adaptation. In general, the first step is to set resizeableActivity to True, then configure the Activity with configChanges, and test it if you want to adapt to a collapsed screen. Finally, you can take it a step further and design another UI for the larger screen, switching between the UI when the screen is folded.

Here are some related references:

  • Google – Build apps for foldable devices
  • Huawei Foldscreen Application Development Guide
  • Samsung Foldable App Development Guide