Reader small image

You're reading from  Android Studio 4.1 Development Essentials – Java Edition

Product typeBook
Published inMay 2021
PublisherPackt
ISBN-139781801815161
Edition1st Edition
Right arrow
Author (1)
Neil Smyth
Neil Smyth
author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor’s degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth

Right arrow

64. Foldable Devices and Multi-Window Support

Foldable devices are coming whether we are ready for them or not (or, in the case of the first Samsung Galaxy Fold, perhaps even before the devices themselves are ready). In preparation for this new category of device, it is important to be aware of some additional steps that should be taken to ensure that your app performs correctly when running on a foldable device.

Fortunately, much of the behavior for supporting foldable devices already exists on Android in the form of Multi-Window support.

64.1 Foldables and Multi-Window Support

When an app is running on a foldable device there is the potential that it will end up sharing the screen with other apps and encountering significant configuration changes (such as the size of the screen changing as the user folds or unfolds the display). If your app is already designed to handle device orientation changes, it will most likely also be able to handle changes caused by screen folding, though thorough testing is recommended.

Multi-window support was originally introduced with Android 7. Unlike previous versions of Android, multi-window support in Android 7 allowed more than one activity to be displayed on the device screen at one time.

Multi-window support in Android provides three different forms of window support. Split-screen mode, available on most phone, foldable and tablet devices, provides a split screen environment where two activities appear either side by side or one above the other. A moveable divider is provided...

64.2 Using a Foldable Emulator

Although at time of writing there are no foldable devices on the market with which to perform app testing, foldable emulators are included with the Android SDK. To create a foldable emulator, select the Android Studio Tools -> AVD Manager menu option, click on the Create Virtual Device button and, from the hardware selection screen, choose one of the Foldable options as highlighted in Figure 64-3 below:

Figure 64-3

After making a foldable selection, continue through the creation process, selecting Android 10 API 29 or newer as the system image.

Once the emulator is up and running, an additional button will appear in the toolbar allowing the emulator to be switched between folded and unfolded configurations:

Figure 64-4

64.3 Entering Multi-Window Mode

Split-screen mode can be entered by displaying the Overview screen, pressing and holding the app icon in the toolbar of a listed app and selecting the Split screen menu option as indicated in Figure 64-5:

Figure 64-5

Once in split-screen mode, the Overview button will change to display two rectangles (marked A in Figure 64-6), the current activity will fill part of the screen (B) and the Overview screen will appear in the adjacent part of the screen allowing the second activity to be selected for display (C):

Figure 64-6

Once the second app has been selected, the screen will be split evenly as illustrated previously in Figure 64-1 above.

To exit split-screen mode, simply drag the divider separating the two activities to a far edge so that only one activity fills the screen, or press and hold the Overview button until it reverts to a single square.

64.4 Enabling and using Freeform Support

Although not officially supported on all devices, it is possible to enable freeform multi-window mode on large screen devices and emulators. To enable this mode, run the following adb command while the emulator is running, or the device is connected:

adb shell settings put global enable_freeform_support 1

After making this change, it may be necessary to reboot the device before the setting takes effect.

Once enabled, an additional option will appear within the Overview screen when performing a long press on the app icon as shown in Figure 64-7:

Figure 64-7

64.5 Checking for Freeform Support

As outlined earlier in the chapter, Google is leaving the choice of whether to enable freeform multi-window mode to the individual Android device manufacturers. Since it only makes sense to use freeform on larger devices, there is no guarantee that freeform will be available on every device on which an app is likely to run. Fortunately all of the freeform specific methods and attributes are ignored by the system if freeform mode is not available on a device, so using these will not cause the app to crash on a non-freeform device. Situations might arise, however, where it may be useful to be able to detect if a device supports freeform multi-window mode. Fortunately, this can be achieved by checking for the freeform window management feature in the package manager. The following code example checks for freeform multi-window support and returns a Boolean value based on the result of the test:

public Boolean checkFreeform() {

   ...

64.6 Enabling Multi-Window Support in an App

The android:resizableActivity manifest file setting controls whether multi-window behavior is supported by an app. This setting can be made at either the application or individual activity levels. The following fragment, for example, configures the activity named MainActivity to support both split-screen and freeform multi-window modes:

<activity

    android:name=".MainActivity"

    android:resizeableActivity="true"

    android:label="@string/app_name"

    android:theme="@style/AppTheme.NoActionBar">

    <intent-filter>

        <action android:name="android.intent.action.MAIN" />

 

        <category android:name="android.intent.category.LAUNCHER" />...

64.7 Specifying Multi-Window Attributes

A number of attributes are available as part of the <layout> element for specifying the size and placement of an activity when it is launched into a multi-window mode. The initial height, width and position of an activity when launched in freeform mode may be specified using the following attributes:

android:defaultWidth – Specifies the default width of the activity.

android:defaultHeight – Specifies the default height of the activity.

android:gravity – Specifies the initial position of the activity (start, end, left, right, top etc.).

Note that the above attributes apply to the activity only when it is displayed in freeform mode. The following example configures an activity to appear with a specific height and width at the top of the starting edge of the screen:

<activity android:name=".MainActivity ">

    <layout android:defaultHeight...

64.8 Detecting Multi-Window Mode in an Activity

Situations may arise where an activity needs to detect whether it is currently being displayed to the user in multi-window mode. The current status can be obtained via a call to the isInMultiWindowMode() method of the Activity class. When called, this method returns a true or false value depending on whether or not the activity is currently full screen:

if (this.isInMultiWindowMode()) {

    // Activity is running in Multi-Window mode

} else {

    // Activity is not in Multi-Window mode

}

64.9 Receiving Multi-Window Notifications

An activity will receive notification that it is entering or exiting multi-window mode if it overrides the onMultiWindowModeChanged() callback method. The first argument passed to this method is true on entering multi-window mode, and false when the activity exits the mode. The new configuration settings are contained within the Configuration object passed as the second argument:

@Override

public void onMultiWindowModeChanged(boolean isInMultiWindowMode,

                                          Configuration newConfig) {

    super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);

    

    if (isInMultiWindowMode) {

     ...

64.10 Launching an Activity in Multi-Window Mode

In the “Android Explicit Intents – A Worked Example” chapter of this book, an example app was created in which an activity uses an intent to launch a second activity. By default, activities launched via an intent are considered to reside in the same task stack as the originating activity. An activity can, however, be launched into a new task stack by passing through the appropriate flags with the intent.

When an activity in multi-window mode launches another activity within the same task stack, the new activity replaces the originating activity within the split-screen or freeform window (the user returns to the original activity via the back button).

When launched into a new task stack in split-screen mode, however, the second activity will appear in the window adjacent to the original activity, allowing both activities to be viewed simultaneously. In the case of freeform mode, the launched activity will appear...

64.11 Configuring Freeform Activity Size and Position

By default, an activity launched into a different task stack while in freeform mode will be positioned in the center of the screen at a size dictated by the system. The location and dimensions of this window can be controlled by passing launch bounds settings to the intent via the ActivityOptions class. The first step in this process is to create a Rect object configured with the left (X), top (Y), right (X) and bottom (Y) coordinates of the rectangle representing the activity window. The following code, for example, creates a Rect object in which the top-left corner is positioned at coordinate (0, 0) and the bottom-right at (100, 100):

Rect rect = new Rect(100, 800, 900, 700);

The next step is to create a basic instance of the ActivityOptions class and initialize it with the Rect settings via the setLaunchBounds() method:

ActivityOptions options = ActivityOptions.makeBasic();

ActivityOptions bounds = options.setLaunchBounds...

64.12 Summary

Android 7 introduced multi-window support, a system whereby more than one activity is displayed on the screen at any one time. This feature now forms the foundation of providing support for foldable devices. The three modes provided by multi-window support are split-screen, freeform and picture-in-picture. In split-screen mode, the screen is split either horizontally or vertically into two panes with an activity displayed in each pane. Freeform mode, which is only supported on certain Android devices, allows each activity to appear in a separate, movable and resizable window. As outlined in this chapter, a number of methods and property settings are available within the Android SDK to detect, respond to and control multi-window behavior within an app.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Studio 4.1 Development Essentials – Java Edition
Published in: May 2021Publisher: PacktISBN-13: 9781801815161
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor’s degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth