'Share Button In Compose

How can I make a share button in compose, which will share a download link,

I tried this but the result was different that I expected.

Here is the code.

 Button(
            onClick = {
                val intent = Intent(Intent.ACTION_SEND)
                    .putExtra("File Download Link", downloadUrl.value)
                    .setType("text/plain")
                context.startActivity(Intent.createChooser(intent, "Share Using"))
            },

and this is how I am getting the downloadUrl value

val downloadUrl = remember { mutableStateOf("") }
val context = LocalContext.current
val launcher =
    rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
        val task = firebaseUpload(uri, context)
        task.addOnCompleteListener {
            if (task.isSuccessful) {
                downloadUrl.value = task.result.toString()
            }
        }
    }

The downloadUrl value is right but when I copied it in the emulator it was copying the modifier.



Solution 1:[1]

you can use this code sample:

  onShareClick = {
                            
                            val context = LocalContext.current
                            
                            val type = "text/plain"
                            val subject = "Your subject"
                            val extraText = "https://www.google.com/codes/${variable.id}"
                            val shareWith = "ShareWith"

                            val intent = Intent(Intent.ACTION_SEND)
                            intent.type = type
                            intent.putExtra(Intent.EXTRA_SUBJECT, subject)
                            intent.putExtra(Intent.EXTRA_TEXT, extraText)

                            ContextCompat.startActivity(
                                context,
                                Intent.createChooser(intent, shareWith),
                                null
                            )
                        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hamed Rahimi