Notes: 08. Enable Drag & Drop Support: Part 2
There are different ways to support multi-tasking in Android! Multi-Window mode is just one of the cool Android features that supports this.
If you want to try out different multi-tasking features, be sure to check out our Implementing Picture in Picture Mode In Android course.
And the last thing you have to do to test your Drag & Drop feature is to enable listening for drops in Droppey.
Start off by opening the AndroidManifest.xml file and add the resizableActivity flag, to enable split screen:
<application
android:resizeableActivity="true"/>
Now that you’ve done that, open the MainActivity.kt file, and add the following code to the onCreate() function:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
enableDropListener()
}
Like before, you’re enabling the drop listener, but you’ll enable it only for the DROP action in this Activity. To do that, build the enableDropListener() function:
private fun enableDropListener() {
val notesDrop = findViewById<TextView>(R.id.notesDrop)
notesDrop.setOnDragListener { _, event ->
if (event.action == ACTION_DROP) {
val notes = event.clipData.getItemAt(0).text
notesDrop.text = notes
}
true
}
}
This should be pretty straightforward. You’re fetching the notesDrop text element, and attaching a drag listener. Within the drag listener, you check if the drag & drop action is the DROP action, and if it is, you read the clip data.
You fetch the clip data at the first position in the list, using getItemAt(0). Once you fetch it, you can get its text and display it in the notesDrop element. Pretty simple and cool! :] Now build and run both of the apps on your phone, start the split-screen mode and drag the notes from one screen to another.
See how easy it is to share data between two applications, or two activities or screens. Using this amazing Multi-Window knowledge, you can build more powerful apps, that let you share data across the system, and speed up your app usage. Well done!