Development (Drag & Drop)

Step 1: Open the project file

Open Android Studio and click Open an Existing Project.

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


Step 2: 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"

Step 3: 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)


Step 4: Store the action type to a variable

Declare a variable to store the action type for the incoming event.

val action: Int = event.getAction()


Step 5: 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
              }


Step 6: Request for drag and drop permission

Finally, request permission to access the data being dragged into the target activity.

requestDragAndDropPermissions(event)


Step 7: Run the app

After building the APK, you can run the optimized note-taking app on a Galaxy Fold or Galaxy Z Fold2, to 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.



If you wish to learn how to implement Copy & Paste for multi-window mode, you may continue with the next topic.