Objective
Learn how to implement drag and drop on a note-taking app when in multi-window mode.
Overview
In Galaxy Fold and it's latest versions, 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, drag & drop is one of the useful features when multitasking. Drag and drop allows you to easily move data from one app to another. 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, Z Fold3, or newer
- 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 (Drag & Drop) Sample Code (19.27 MB)
Start your project
Open Android Studio and click Open an Existing Project.
Locate the downloaded Android Project (SimpleNotes_DragDrop) 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 or in pop-up view, and adapt different screen size.
android:resizeableActivity= "true"
NoteIf this attribute is set to true
, the activity can be launched in split-screen and freeform modes. Otherwise, it will disable multi-window display. Remember to handle the changes required to fit your UX in small windows when in multi-window mode. For this Code Lab, you do not need to worry about the UX as it is already handled.
Register a drag event listener
In NewNote.kt
, register a drag event listener object by calling setOnDragListener
for both title and description view of the app.
title.setOnDragListener(onDragListener)
desc.setOnDragListener(onDragListenerDescription)
Store the action type to a variable
Declare a variable to store the action type for the incoming event.
val action: Int = event.getAction()
Get and drop the text data
Inside the DragEvent.ACTION_DROP
, get the item from ClipData
and check the MIME type. If the MIME type is set to text/plain
, get the text value from the item object and allow the operation of drop. Otherwise, simply show a toast message.
val item: Item = event.getClipData().getItemAt(0)
var mType = event.clipDescription.getMimeType(0)
if(mType == "text/plain" || mType== "text/html"){
// Gets the text data from the item.
dragData = item.text.toString()
}else{
Toast.makeText(applicationContext,"Operation not allowed"+mType,Toast.LENGTH_LONG).show()
return@OnDragListener true
}
TipIn this Code Lab, to simplify the demonstration of implementing drag and drop, MIME type or media type is set to plain text only.
Run the app
After building the APK, you can run the optimized note-taking app and start dragging and dropping texts between apps in multi-window mode. If you don’t have any physical device, you can also test it on a Remote Test Lab device.
NoteWatch this tutorial video and know how to easily test your app via Remote Test Lab.
You're done!
Congratulations! You have successfully achieved the goal of this Code Lab. Now, you can implement drag and drop in your app for your foldable device by yourself! If you're having trouble, you may download this file:
Multi-Window (Drag & Drop) Complete Code (19.33 MB)
To learn more about developing apps for Galaxy Foldable devices, visit:
www.developer.samsung.com/galaxy-z