Clipboard & Share in Compose: Copy, Paste & Intent Sharing Guide

Published: (March 1, 2026 at 08:18 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

LocalClipboardManager: Copy and Paste

val clipboardManager = LocalClipboardManager.current

// Copy text to clipboard
Button(onClick = {
    clipboardManager.setText(AnnotatedString("Hello, Android!"))
}) {
    Text("Copy to Clipboard")
}

// Read from clipboard
val pastedText = remember { mutableStateOf("") }
Button(onClick = {
    pastedText.value = clipboardManager.getText()?.text ?: ""
}) {
    Text("Paste from Clipboard")
}

Providing User Feedback with Snackbar

val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()

Column {
    Button(onClick = {
        clipboardManager.setText(AnnotatedString("Copied!"))
        scope.launch {
            snackbarHostState.showSnackbar("Text copied to clipboard")
        }
    }) {
        Text("Copy")
    }

    SnackbarHost(hostState = snackbarHostState)
}

Intent Sharing with createChooser

val context = LocalContext.current

Button(onClick = {
    val sendIntent = Intent().apply {
        action = Intent.ACTION_SEND
        putExtra(Intent.EXTRA_TEXT, "Check out this app!")
        putExtra(Intent.EXTRA_SUBJECT, "Amazing App")
        type = "text/plain"
    }
    val chooserIntent = Intent.createChooser(sendIntent, "Share via")
    context.startActivity(chooserIntent)
}) {
    Text("Share Text")
}

Sharing Images with Intent

val context = LocalContext.current

Button(onClick = {
    val imageUri = Uri.parse("content://com.example.app/images/sample.jpg")
    val shareIntent = Intent().apply {
        action = Intent.ACTION_SEND
        putExtra(Intent.EXTRA_STREAM, imageUri)
        type = "image/*"
    }
    val chooser = Intent.createChooser(shareIntent, "Share Image")
    context.startActivity(chooser)
}) {
    Text("Share Image")
}

Receiving Shared Content

Manifest Declaration

Handling Received Content in Compose

val intent = LocalContext.current.currentActivity?.intent
val sharedText = intent?.getStringExtra(Intent.EXTRA_TEXT) ?: ""
val sharedUri = intent?.getParcelableExtra(Intent.EXTRA_STREAM)

Text("Received: $sharedText")

Best Practices

  • Always provide visual feedback (Snackbar, Toast) after clipboard operations.
  • Use createChooser() to let users select their preferred sharing app.
  • Handle missing clipboard data gracefully.
  • Request proper permissions for accessing shared files.
  • Test with multiple sharing apps (WhatsApp, Telegram, Email, etc.).

Explore more Android development patterns: 8 Android App Templates →

0 views
Back to Blog

Related posts

Read more »