Configure an App to Enable Copy and Paste in Multi-Window


Objective

Learn how to implement Copy and Paste on a note-taking app when in Multi-Window mode.

Overview

In Galaxy Fold, Z Fold2, or Z Fold3, the advantage of its larger display is to split its screen and simultaneously use up to three apps. In multi-window mode, you can split the screen with one window being the main focus, and the other two windows off to the side. All three windows are active, not just the largest one. You can multitask in either landscape or portrait mode, giving you even more flexibility.

When using multi-window, copy & paste is one of the useful features when multitasking. Copy and paste is a common feature used by many, to duplicate an object and place it in a desired location. To provide users with better multitasking experience on Samsung's foldable devices, developers need to optimize their apps to work on multi-window mode.

Set up your environment

You will need the following:

  • Java SE Development Kit (JDK) 8 or later

  • Android Studio (latest version recommended)

  • Samsung Galaxy Fold, Z Fold2, or Z Fold3

  • Remote Test Lab (if physical device is not available)

    Requirements:

    • Samsung account
    • Java Runtime Environment (JRE) 7 or later with Java Web Start
    • Internet environment where port 2600 is available

Sample Code

Here is a sample code for you to start coding in this Code Lab. Download it and start your learning experience!

Multi-Window (Copy & Paste) Sample Code
(19.48 MB)

Start your project

Open Android Studio and click Open an Existing Project.

Locate the downloaded Android Project (SimpleNotes_CopyPaste) from the directory and click OK.

Make the app resizable

To ensure that the app works in multi-window mode, you need to add an attribute in the manifest’s <activity> element. If you set android:resizeableActivity to true, the activity can be launched in multi-window, pop-up view and adapt different screen size.


android:resizeableActivity= "true"

Get a reference to ClipboardManager class

ClipboardManager provides methods to get and set the current primary clipboard data expressed as a ClipData object, which defines the protocol for data exchange between applications. In NewNote.kt, get a reference to ClipboardManager class by invoking getSystemService(CLIPBOARD_SERVICE).


val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager

Complete the copy operation

Now, create a new ClipData of simple text using newPlainText of ClipboardManager and pass it on setPrimaryClip to complete the copy operation.


val clip: ClipData = ClipData.newPlainText("simple text", textData)
clipboard.setPrimaryClip(clip)

Get the text data from the clipboard

Return the ClipData by getting the primaryClip data from the clipboard. From the primaryClip, acquire the item using getItemAt(0).


val item = clipboard.primaryClip?.getItemAt(0)

Run the app

After building the APK, you can run the optimized app and test it by copying and pasting texts between apps when in multi-window mode. If you don’t have any physical device, you can also test it on a Remote Test Lab device.


You're done!

Congratulations! You have successfully achieved the goal of this Code Lab. Now, you can implement multi-window features such as copy and paste in your app by yourself! If you're having trouble, you may download this file:

Multi-Window (Copy & Paste) Complete Code
(19.53 MB)

To learn more about developing apps for Galaxy Foldable devices, visit: www.developer.samsung.com/galaxy-z