I was trying to add onClickListener to a button inside a RecyclerView that wii copy a string but it says getSystemService(CLIPBOARD_SERVICE) is not available.
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)getSystemService(CLIPBOARD_SERVICE);
String text;
text = EditText.getText().toString();
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Text Copied", Toast.LENGTH_SHORT).show();
}
});
}
You need a Context
in order to do that. Perform:
...
public void onClick(View v) {
myClipboard = (ClipboardManager) v.getContext().getSystemService(CLIPBOARD_SERVICE);
...
}