Resizable Apps & Multi-Window Support in Android

Mar 30 2021 · Kotlin 1.4, Android 11, Android Studio 4

Part 1: Resizable Apps & Multi-Window Support in Android

08. Enable Drag & Drop Support: Part 2

Episode complete

About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 07. Enable Drag & Drop Support: Part 1

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

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.

Transcript: 08. Enable Drag & Drop Support: Part 2

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!