Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Android Studio 4.2 Development Essentials - Kotlin Edition

You're reading from  Android Studio 4.2 Development Essentials - Kotlin Edition

Product type Book
Published in Aug 2021
Publisher Packt
ISBN-13 9781803231549
Pages 804 pages
Edition 1st Edition
Languages
Author (1):
Neil Smyth Neil Smyth
Profile icon Neil Smyth

Table of Contents (94) Chapters

1. Introduction 2. Setting up an Android Studio Development Environment 3. Creating an Example Android App in Android Studio 4. Creating an Android Virtual Device (AVD) in Android Studio 5. Using and Configuring the Android Studio AVD Emulator 6. A Tour of the Android Studio User Interface 7. Testing Android Studio Apps on a Physical Android Device 8. The Basics of the Android Studio Code Editor 9. An Overview of the Android Architecture 10. The Anatomy of an Android Application 11. An Introduction to Kotlin 12. Kotlin Data Types,Variables and Nullability 13. Kotlin Operators and Expressions 14. Kotlin Flow Control 15. An Overview of Kotlin Functions and Lambdas 16. The Basics of Object Oriented Programming in Kotlin 17. An Introduction to Kotlin Inheritance and Subclassing 18. An Overview of Android View Binding 19. Understanding Android Application and Activity Lifecycles 20. Handling Android Activity State Changes 21. Android Activity State Changes by Example 22. Saving and Restoring the State of an Android Activity 23. Understanding Android Views, View Groups and Layouts 24. A Guide to the Android Studio Layout Editor Tool 25. A Guide to the Android ConstraintLayout 26. A Guide to Using ConstraintLayout in Android Studio 27. Working with ConstraintLayout Chains and Ratios in Android Studio 28. An Android Studio Layout Editor ConstraintLayout Tutorial 29. Manual XML Layout Design in Android Studio 30. Managing Constraints using Constraint Sets 31. An Android ConstraintSet Tutorial 32. A Guide to using Apply Changes in Android Studio 33. An Overview and Example of Android Event Handling 34. Android Touch and Multi-touch Event Handling 35. Detecting Common Gestures Using the Android Gesture Detector Class 36. Implementing Custom Gesture and Pinch Recognition on Android 37. An Introduction to Android Fragments 38. Using Fragments in Android Studio - An Example 39. Modern Android App Architecture with Jetpack 40. An Android Jetpack ViewModel Tutorial 41. An Android Jetpack LiveData Tutorial 42. An Overview of Android Jetpack Data Binding 43. An Android Jetpack Data Binding Tutorial 44. An Android ViewModel Saved State Tutorial 45. Working with Android Lifecycle-Aware Components 46. An Android Jetpack Lifecycle Awareness Tutorial 47. An Overview of the Navigation Architecture Component 48. An Android Jetpack Navigation Component Tutorial 49. An Introduction to MotionLayout 50. An Android MotionLayout Editor Tutorial 51. A MotionLayout KeyCycle Tutorial 52. Working with the Floating Action Button and Snackbar 53. Creating a Tabbed Interface using the TabLayout Component 54. Working with the RecyclerView and CardView Widgets 55. An Android RecyclerView and CardView Tutorial 56. A Layout Editor Sample Data Tutorial 57. Working with the AppBar and Collapsing Toolbar Layouts 58. An Android Studio Primary/Detail Flow Tutorial 59. An Overview of Android Intents 60. Android Explicit Intents – A Worked Example 61. Android Implicit Intents – A Worked Example 62. Android Broadcast Intents and Broadcast Receivers 63. An Introduction to Kotlin Coroutines 64. An Android Kotlin Coroutines Tutorial 65. An Overview of Android Services 66. Implementing an Android Started Service – A Worked Example 67. Android Local Bound Services – A Worked Example 68. Android Remote Bound Services – A Worked Example 69. An Android Notifications Tutorial 70. An Android Direct Reply Notification Tutorial 71. Foldable Devices and Multi-Window Support 72. An Overview of Android SQLite Databases 73. The Android Room Persistence Library 74. An Android TableLayout and TableRow Tutorial 75. An Android Room Database and Repository Tutorial 76. Accessing Cloud Storage using the Android Storage Access Framework 77. An Android Storage Access Framework Example 78. Video Playback on Android using the VideoView and MediaController Classes 79. Android Picture-in-Picture Mode 80. An Android Picture-in-Picture Tutorial 81. Making Runtime Permission Requests in Android 82. Android Audio Recording and Playback using MediaPlayer and MediaRecorder 83. Printing with the Android Printing Framework 84. An Android HTML and Web Content Printing Example 85. A Guide to Android Custom Document Printing 86. An Introduction to Android App Links 87. An Android Studio App Links Tutorial 88. A Guide to the Android Studio Profiler 89. An Android Biometric Authentication Tutorial 90. Creating, Testing and Uploading an Android App Bundle 91. An Overview of Android Dynamic Feature Modules 92. An Android Studio Dynamic Feature Tutorial 93. An Overview of Gradle in Android Studio Index

71. 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.

71.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...

71.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 71-3 below:

Figure 71-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 71-4

71.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 71-5:

Figure 71-5

Once in split-screen mode, the Overview button will change to display two rectangles (marked A in Figure 71-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 71-6

Once the second app has been selected, the screen will be split evenly as illustrated previously in Figure 71-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.

71.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 71-7:

Figure 71-7

71.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:

fun checkFreeform(): Boolean {

   &...

71.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" />...

71.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...

71.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

}

71.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 fun onMultiWindowModeChanged(isInMultiWindowMode: Boolean,

                                newConfig: Configuration?) {

    super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig)

    

    if (isInMultiWindowMode) {

        // Activity has entered multi-window mode

 ...

71.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...

71.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):

val rect = Rect(0, 0, 100, 100)

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

val options = ActivityOptions.makeBasic()

val bounds = options.setLaunchBounds(rect)

Finally, the ActivityOptions...

71.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.2 Development Essentials - Kotlin Edition
Published in: Aug 2021 Publisher: Packt ISBN-13: 9781803231549
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.
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}