Home > Back-end >  CopytoClipboard not working in Adapter to show in recyclerview
CopytoClipboard not working in Adapter to show in recyclerview

Time:07-15

I have a recyclerview with adapter (to show external strings)and I'm trying to click a button "copy" so that it copies the strings in view to the clipboard.

CardLayout.xml Button to use as copy button is android:id="@ id/copyactivity"

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    app:cardBackgroundColor="@color/white"
    app:cardCornerRadius="0dp"
    app:cardElevation="0dp"
    app:cardMaxElevation="3dp"
    app:cardPreventCornerOverlap="true"
    app:cardUseCompatPadding="true"
    tools:context="ui.CardLayout">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >


        <TextView
            android:id="@ id/idActivityName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="14dp"
            android:layout_marginRight="10dp"
            android:text="@string/placeholdertasktitle"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@ id/idActivityDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/idActivityName"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="30dp"
            android:drawablePadding="2dp"
            android:text="@string/let_s_get_started_creating_a_photo_album_for_you_so_you_can_share_with_the_kids_in_the_near_future"
            android:textColor="@color/black"
            android:textSize="15sp"
            tools:ignore="UnknownId" />


        <View
            android:id="@ id/divider"
            android:layout_width="380dp"
            android:layout_height="2dp"
            android:layout_below="@id/idActivityDescription"
            android:layout_alignStart="@id/idActivityName"
            android:layout_centerHorizontal="false"
            android:layout_centerVertical="false"
            android:background="#EDEDED" />

        <Button
            android:id="@ id/saves"
            style="@style/cardbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/divider"
            android:layout_alignStart="@id/idActivityName"
            android:background="@drawable/cardbutton"
            android:contentDescription="@string/saves"
            android:stateListAnimator="@null"
            android:text="Save"
            android:textAlignment="textStart"
            android:textColor="#595959"
            android:textSize="12sp"
            app:icon="@drawable/ic_saves_blank"
            app:iconGravity="textStart"
            app:iconTint="#595959" />

        <Button
            android:id="@ id/calendar"
            style="@style/cardbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/divider"
            android:layout_marginStart="35dp"
            android:layout_marginEnd="35dp"
            android:layout_toStartOf="@ id/copyactivity"
            android:layout_toEndOf="@id/saves"
            android:background="@drawable/cardbutton"
            android:contentDescription="@string/saves"
            android:stateListAnimator="@null"
            android:text="@string/calendar"
            android:textAlignment="textStart"
            android:textColor="#595959"
            android:textSize="12sp"
            app:icon="@drawable/ic_calendar"
            app:iconGravity="textStart"
            app:iconTint="#595959" />

        <Button
            android:id="@ id/copyactivity"
            style="@style/cardbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/divider"
            android:layout_alignEnd="@id/divider"
            android:background="@drawable/cardbutton"
            android:contentDescription="@string/saves"
            android:stateListAnimator="@null"
            android:text="@string/copy"
            android:textAlignment="textStart"
            android:textColor="#595959"
            android:textSize="12sp"
            app:icon="@drawable/ic_copytext"
            app:iconGravity="textStart"
            app:iconTint="#595959" />

    </RelativeLayout>
</androidx.cardview.widget.CardView>

And this is my TaskAdapter.kt

package com.example.what2do_v6.Adapter

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context.CLIPBOARD_SERVICE
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.recyclerview.widget.RecyclerView
import com.example.what2do_v6.Model.TaskResponse
import com.example.what2do_v6.R
import com.example.what2do_v6.Repository.TaskRepository
import com.example.what2do_v6.databinding.CardLayoutBinding
import com.example.what2do_v6.ui.CardLayout
import android.content.Context as ContentContext


class TaskAdapter: RecyclerView.Adapter<TaskAdapter.TaskViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding = CardLayoutBinding.inflate(layoutInflater, parent, false)
        return TaskViewHolder(binding)



            }

    override fun onBindViewHolder(holder: TaskViewHolder, position: Int) {
        holder.bind(TaskRepository.list[position])



    }
    override fun getItemCount(): Int {
        return TaskRepository.list.size
    }
    class TaskViewHolder(private val binding: CardLayoutBinding) : RecyclerView.ViewHolder(binding.root){
        var bCopyText: Button? = null
        fun bind(task: TaskResponse) {
            binding.idActivityName.text = task.title
            binding.idActivityDescription.text = task.description

            bCopyText = findViewById<View>(R.id.copyactivity) as Button
            val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
            bCopyText!!.setOnClickListener {
                val clipData = ClipData.newPlainText("", task.description,)
                clipboardManager.setPrimaryClip(clipData)
                Toast.makeText(RecyclerView, "message...", Toast.LENGTH_SHORT).show()
            }

        }

        private fun getSystemService(clipboardService: String): Any {

        }


    }


}

There are two lines in my code that are throwing up problems:

bCopyText = findViewById<View>(R.id.copyactivity) as Button

and

Toast.makeText(RecyclerView, "message...", Toast.LENGTH_SHORT).show()

I've tried different variances of the two above lines and research many articles on StackOverflow but with no winner, can anyone help?

CodePudding user response:

Managed to get this working utilising help from Gobu:

class TaskViewHolder(private val binding: CardLayoutBinding) : RecyclerView.ViewHolder(binding.root){

        fun bind(task: TaskResponse) {
            binding.idActivityName.text = task.title
            binding.idActivityDescription.text = task.description
            binding.copyactivity

            binding.copyactivity.setOnClickListener {
                val clipboardManager = itemView.context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
                val clipData = ClipData.newPlainText("", task.title  "\n\n"   task.description)
                clipboardManager.setPrimaryClip(clipData)
                Toast.makeText(itemView.context, "Activity copied to clipboard", Toast.LENGTH_SHORT).show()

            }

        }

CodePudding user response:

First of all, if you're using ViewBinding, why would you declare a variable for your button? Just get it from the binding.

Second, it's giving you an error, because you're making sure (!!) that the button you declare isn't null, but most likely it is, because you're trying to get the ID of a view you don't have.

Third, RecyclerView is not a context, if you are using ViewBinding, you get the context from binding.root.context

  • Related