Reputation: 13483
I'm letting the user long click on a TextView
and that will copy the text of the TextView
to their Clipboard
.
I want it so that before it will actually copy the text of the TextView
to their Clipboard
, it will check if the last Clip
on their Clipboard
is the different than the text.
Here is the code:
ClipboardManager clipboard =(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
String text = textView.getText().toString();
if ( !(clipboard.getPrimaryClip().toString().equals(text)) )
// if ( !(clipboard.getPrimaryClip().equals(text)) )
{
clipboard.setPrimaryClip(ClipData.newPlainText("newClipName", text));
Toast.makeText(getApplicationContext(),"Copied to clipboard.", 0).show();
}
I can only imagine that the method getPrimaryClip()
is not returning a String
and toString()
also doesn't work. How can I get the most recent Clip
as a String
?
Upvotes: 1
Views: 267
Reputation: 13483
ClipboardManager clipboard=(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
String text = textView.getText().toString();
// item is the most recent Clip from the Clipboard
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
// Gets the clipboard as text.
String clipText = item.getText().toString(); // getText() returns CharSequence
if ( !(clipText.equals(text)) )
{
clipboard.setPrimaryClip(ClipData.newPlainText("newClipName", text));
Toast.makeText(getApplicationContext(),"Copied to clipboard.", 0).show();
}
Upvotes: 1