how to copy text on button click in android code example

Example 1: how to add copy to clipboard button android

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
 ClipData clip = ClipData.newPlainText(label, text);
 clipboard.setPrimaryClip(clip);

Example 2: kotlin copy text android 10

fun copyText(text:String){
            val myClipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val myClip: ClipData = ClipData.newPlainText("Label", text)
            myClipboard.setPrimaryClip(myClip)
        }