0

Hey I'm trying to copy text from recyclerview items into the clipboard its works when I try to do this inside an activity but when I try the code inside the viewholder i get Unresolved reference: CLIPBOARD_SERVICE erorr here is the code:

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    val currentitem = userList[position]

    holder.username.text = currentitem.name
    holder.app.text = currentitem.app
    holder.password.text = currentitem.password

    holder.copy.setOnClickListener(){
        val clipboardManager = getSystemService(holder.itemView.context.CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText("text", currentitem.password)
        clipboardManager.setPrimaryClip(clipData)
        Toast.makeText(holder.itemView.context, "Text copied to clipboard", Toast.LENGTH_LONG).show()
    }

enter image description here

zoids3
  • 65
  • 10

1 Answers1

1

Change holder.itemView.context.CLIPBOARD_SERVICE with Context.CLIPBOARD_SERVICE and Add holder.itemView.context.getSystemService instead of simple getSystemService

        val clipboardManager = holder.itemView.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText("text", "1234567890")
        clipboardManager.setPrimaryClip(clipData)
        Toast.makeText(holder.itemView.context, "Text copied to clipboard", Toast.LENGTH_LONG).show()
Android Geek
  • 8,956
  • 2
  • 21
  • 35