Clipboard & Share in Compose: Copy, Paste & Intent Sharing Guide
Handling clipboard and intent sharing is essential for modern Android apps. In Jetpack Compose, combining LocalClipboardManager, Intent.ACTION_SEND, and proper feedback mechanisms creates a seamless user experience.
LocalClipboardManager: Copy and Paste
Use LocalClipboardManager to read/write clipboard data directly from Compose:
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
Always notify users of clipboard operations with visual feedback:
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
Share content via Intent.ACTION_SEND:
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
Share images by providing a URI:
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
Declare an intent-filter to receive shared data:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
Handle received content in your Compose screen:
val intent = LocalContext.current.currentActivity?.intent
val sharedText = intent?.getStringExtra(Intent.EXTRA_TEXT) ?: ""
val sharedUri = intent?.getParcelableExtra<Uri>(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.)
Master clipboard and sharing patterns to build user-friendly Android apps that integrate seamlessly with the ecosystem.
Explore more Android development patterns: 8 Android App Templates → https://myougatheax.gumroad.com
Top comments (0)