0

I have a custom ListView adapter which contains 2 Textviews for each item , On long click on the item I want to have the option to copy the text from 1 of these Textviews to the clipboard, the question is how can i get this Textview's text?

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.copy:
                Toast.makeText(getActivity(),"Text copied to clipboard.", Toast.LENGTH_SHORT).show();
    //HOW CAN I GET THE TEXT?
                mode.finish();
                return true;
            case R.id.share:
                return false;
            default:
                return false;
        }
    }
james
  • 221
  • 1
  • 5
  • 14

1 Answers1

0
ListView list = (ListView) findViewById(R.id.yourList);    
list.setOnItemLongClickListener(new OnItemLongClickListener() {
public void onItemLongClick(AdapterView<?> a, View v, int position,long id) {
                TextView yourFirstTextView = (TextView) v.findViewById(R.id.yourFirstTextViewId);
                TextView yourSecondTextView = (TextView) v.findViewById(R.id.yourSecondTextView);
                copyTextToClipboard(yourFirstTextView);//if you want to copy your first textview
                copyTextToClipboard(yourSecondTextView);//if you want to copy your second textview
});

public void copyTextToClipboard(TextView txtView){
     int sdk = android.os.Build.VERSION.SDK_INT;
     if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
         android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
         clipboard.setText(txtView.getText().toString());
     } else {
         android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
         android.content.ClipData clip = android.content.ClipData.newPlainText("text label",txtView.getText().toString());
         clipboard.setPrimaryClip(clip);
     }
}

I don't test this code but it may work.

zeTechMoy
  • 300
  • 1
  • 3
  • 9
  • Hi , i don't want to copy the text on LongClick , I want the user to decide if he wants to copy or share the text. i have already created a cab (contextual ActionBar) on LongClick which displays these 2 buttons in the action bar. The problem is I don't know how to access those TextViews from within these buttons. – james Sep 16 '14 at 17:34
  • You should not access the text views. You should get data from your array adapter. The only thing you have to 'remember' is the position of the clicked view. So put that in a variable. – greenapps Sep 16 '14 at 18:00
  • Ok i'll try that,And the copyTextToClipboard you gave me is deprecated what is the new method? – james Sep 16 '14 at 18:36
  • I edited my post and add the exact copyTextToClipBoard method – zeTechMoy Sep 16 '14 at 19:03
  • the method copyTextToClipboard should receive a context also. it should be context.getSystemService. but you helped me , thank you. – james Sep 16 '14 at 20:05
  • @james can I get the code you used finally. I need same functionality of copy text from custom menu. can u please help me . am new to android – Khaleel Mar 03 '15 at 12:06
  • private void copyText(Context context) { android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); android.content.ClipData clip = android.content.ClipData.newPlainText("text label", arrayList.get(intHolder)); clipboard.setPrimaryClip(clip); } hope it helps, of course in order to use it you have to call it with your context. – james Mar 03 '15 at 18:38