Android Copy Text To Clipboard

Kevin FOO
Mar 16, 2021

--

If your app has a long gibberish text like Bitcoin address or Ethereum address. Best to put a button to copy the entire string to clipboard rather than letting the user manually copying it.

EditText and ImageButton
val editText: EditText = findViewById(R.id.editText)
val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val button: ImageButton = findViewById(R.id.imageButton)
button.setOnClickListener {
val
text = editText.text
val clipData = ClipData.newPlainText("text", text)
clipboardManager.setPrimaryClip(clipData)
Toast.makeText(this, "Copied to clipboard", Toast.LENGTH_LONG).show()
}

< Back to all the stories I had written

--

--

Kevin FOO
Kevin FOO

Written by Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.

No responses yet