I've implemented a recycler card view and want to use a button click method to copy the content of cardView. There aretwo texts in cardview and i want to copy the content only, different for different cards. How can I do this? Here is my Cardview adapter.
public static class ViewHolder extends RecyclerView.ViewHolder{
Button copyButton;
Button shareButton;
TextView title;
TextView content;
public ViewHolder(View itemView) {
super(itemView);
this.title = (TextView)itemView.findViewById(R.id.card_title);
this.content = (TextView)itemView.findViewById(R.id.card_content);
this.copyButton= (Button)itemView.findViewById(R.id.copyButton);
this.shareButton=(Button)itemView.findViewById(R.id.shareButton);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(cardItems.get(position).title);
holder.content.setText(cardItems.get(position).content);
holder.copyButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
myClip = ClipData.newPlainText("label", v.content.getText().toString());
myClipboard.setPrimaryClip(myClip);
Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();
}
});
holder.shareButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "This message is being sent to another app");
v.getContext().startActivity(Intent.createChooser(share, "Share Text"));
}
});
}
v.content.getText().toString(), content cannot be resolved.
Try the following
Copy to clipboard the content of a cardview (recyclerview ) Try this code.
The variable v is a
Button
, it not have a text, you have to do it in yourEditText
orTextView
(where you have your text to be copied)