Implementing drag and drop

Here, in the last section of this chapter, we will show you how to implement the drag and drop feature. It's a feature that you will probably need in most applications containing data in lists. Using lists is not mandatory for performing drag and drop, because you can drag anything (view) and release it anywhere where a proper listener is defined. For a better understanding of what we are talking about, we will show you an example of how to implement it.

Let's define a view. On that view, we will set a long press listener that will trigger the drag and drop operation:

 view.setOnLongClickListener { val data = ClipData.newPlainText("", "") val shadowBuilder = View.DragShadowBuilder(view) view.startDrag(data, shadowBuilder, ...

Get Mastering Android Development with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.