Skip to content

Instantly share code, notes, and snippets.

@pflammertsma
Last active December 16, 2021 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pflammertsma/8733d46fa5c3ad5813ab3e083bceeb5a to your computer and use it in GitHub Desktop.
Save pflammertsma/8733d46fa5c3ad5813ab3e083bceeb5a to your computer and use it in GitHub Desktop.
Sample of DragStartHelper
/* Copyright 2021 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
// Make a view draggable to share a file. DragStartHelper takes care of
// intercepting drag gestures and attaching the listener.
DragStartHelper(draggableView) { view, _ ->
// Sets the appropriate MIME types automatically.
val dragClipData = ClipData.newUri(contentResolver, "File", fileUri)
// Set the visual look of the dragged object.
// Can be extended and customized; we use the default here.
val dragShadow = View.DragShadowBuilder(view)
// Starts the drag. Note that the global flag allows for cross-app dragging.
view.startDragAndDrop(
dragClipData,
dragShadow,
null, // Optional extra local state information
// Since this is a "content:" URI and not just plain text, we can use the
// DRAG_FLAG_GLOBAL_URI_READ to allow other apps to read from our content
// provider. Without it, other apps won't receive the drag events.
DRAG_FLAG_GLOBAL or DRAG_FLAG_GLOBAL_URI_READ)
)
}.attach()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment