Search code examples
javaandroidandroid-recyclerviewcopy-paste

How to use Clipboard service in Adapter class of a RecyclerView


I am making an android app in which I am using RecyclerView to display text, I want to copy the text of TextView with the help of an button. But when I am using getSystemService(Context.CLIPBOARD_SERVICE), it is giving me error cannot resolve method. Here is my Adapter class,

package in.codemantri.quotes;

import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private String[] data;
    public MyAdapter(String[] data) {
        this.data=data;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View view=inflater.inflate(R.layout.shayari_layout, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {

        final String mydata=data[position];
        holder.textView.setText(mydata);
        holder.btnWhatsapp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent whatsappIntent = new Intent();
                whatsappIntent.setAction(Intent.ACTION_SEND);
                whatsappIntent.putExtra(Intent.EXTRA_TEXT, mydata);
                whatsappIntent.setType("text/plain");
                whatsappIntent.setPackage("com.whatsapp");
                try {
                    holder.itemView.getContext().startActivity(whatsappIntent);
                }
                catch (android.content.ActivityNotFoundException ex) {

                }
            }
        });
        holder.btnFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent facebookIntent = new Intent();
                facebookIntent.setAction(Intent.ACTION_SEND);
                facebookIntent.putExtra(Intent.EXTRA_TEXT, mydata);
                facebookIntent.setType("text/plain");
                facebookIntent.setPackage("com.facebook.katana");
                try {
                    holder.itemView.getContext().startActivity(facebookIntent);
                }
                catch (android.content.ActivityNotFoundException ex) {

                }
            }
        });
        holder.btnFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent facebookIntentLite = new Intent();
                facebookIntentLite.setAction(Intent.ACTION_SEND);
                facebookIntentLite.putExtra(Intent.EXTRA_TEXT, mydata);
                facebookIntentLite.setType("text/plain");
                facebookIntentLite.setPackage("com.facebook.lite");
                try {
                    holder.itemView.getContext().startActivity(facebookIntentLite);
                }
                catch (android.content.ActivityNotFoundException ex) {

                }
            }
        });
        // Share Button Implemantation
        holder.btnShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent shareIntent=new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_TEXT, mydata);
                shareIntent.setType("text/plain");
                shareIntent=Intent.createChooser(shareIntent, "Share via");
                holder.itemView.getContext().startActivity(shareIntent);
            }
        });
        holder.btnCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager clipboardManager=getSystemService(Context.CLIPBOARD_SERVICE); //Getting Error Here
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.length;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView btnWhatsapp, btnFacebook, btnShare, btnCopy;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.shayari_textView);
            btnWhatsapp=itemView.findViewById(R.id.btn_whatsapp);
            btnFacebook=itemView.findViewById(R.id.btn_facebook);
            btnShare=itemView.findViewById(R.id.btn_share);
            btnCopy=itemView.findViewById(R.id.btn_copy);
        }
    }
}

Solution

  • In an adapter class, you need to get the context from the parent activity in order to use getSystemService. You can get the context in multiple ways, but I got it from the recyclerview itemView.

    Simply put itemView.context in front of getSystemService.

    Kotlin Code:

    val clipboard = itemView.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    val clip: ClipData = ClipData.newPlainText("simple text", "Hello, World!")
    clipboard.setPrimaryClip(clip)