1

How to copy the text from ListView?For copying text i am usin clipboard manager.But,It's not working. here is my code :

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    String message = mMessagelist.get(arg2).getMessage_text().toString();

    if (true) {

        CLIPBOARD_TEXT = ((TextView) findViewById(R.id.message))
                .getText().toString();
        alert.showAlertDialog(MessagesActivity.this, "",
                "Your text is copied to clipboard", false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("simple text",
                    CLIPBOARD_TEXT);
            clipboard.setPrimaryClip(clip);
        } else {
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setText(CLIPBOARD_TEXT);
        }

    }
    return true;
} 

when user long click on item i am just showing a popup and copy the text but it's not working..Please help me.

Android_dev
  • 320
  • 1
  • 7
  • 26

2 Answers2

0
CLIPBOARD_TEXT = ((TextView) findViewById(R.id.message)).getText().toString();

try to split it in parts.

String CLIPBOARD_TEXT;
TextView tvMsg = (TextView) findViewById(R.id.message);
CLIPBOARD_TEXT = tvMsg.getText().toString();

and

ClipData clip = ClipData.newPlainText("simple text", CLIPBOARD_TEXT);

to

ClipData clip = ClipData.newPlainText(CLIPBOARD_TEXT, CLIPBOARD_TEXT);

hope that works.

The Badak
  • 2,010
  • 2
  • 16
  • 28
  • but here my problem is when i am debug an logclick on listitem this onItemLongClickListener not calling.but i am calling longclicklistener inside of oncrete mMessagesListView.setOnItemLongClickListener(MessagesActivity.this); – Android_dev Jan 24 '14 at 07:53
0

Please clarify your long click listener is not working or copying to the clipboard is not working

for copying just use this

 ClipData myClip;
 String text = "hello world";
 myClip = ClipData.newPlainText("text", text);
 myClipboard.setPrimaryClip(myClip);