0

I have a textView and I made its text copyable and then Override onCreateContextMenu:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    //user has long pressed your TextView
    menu.add(0, v.getId(), 0, "Copy");
    TextView yourTextView = (TextView) v;
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    clipboard.setText(yourTextView.getText());

}

I want to show toast "copied to clipboard" after menu is clicked, how can I do that? And my second question is why ClipManager is deprecated? what to use instead?

Suzi
  • 89
  • 4
  • 16

1 Answers1

1
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    //user has long pressed your TextView
    menu.add(0, v.getId(), 0, "Copy");
    TextView yourTextView = (TextView) v;
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

    clipboard.setText(yourTextView.getText());
}

    @Override
public boolean onContextItemSelected(MenuItem item) {

    super.onContextItemSelected(item);

    if (item.getTitle() == "Copy") {
        Toast.makeText(getApplicationContext(), "copied to clipboard", Toast.LENGTH_LONG).show();

    }
    return true;
}

 } 

for SDK>=15 ,this will work

   ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
     ClipData clip = ClipData.newPlainText("label for text", "text to copy");
     clipboard.setPrimaryClip(clip);
Suzi
  • 89
  • 4
  • 16
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • I want ti show toast after I click the menu this code shows toast as soon as menu appear – Suzi Sep 26 '15 at 15:28
  • It does not work, first there is missing return statement, and after I fix it says "method does not override from its superclass – Suzi Sep 26 '15 at 15:43