Copy TextView on Clipboard when pressed

2

I want to make that when I clicked on a TextView in andorid studio it was copied to the clipboard, I tried to put the android:longClickable="true" line in the text view tag as I read on a page, but nothing.

I have changed in the android documentation and I have tried to put this code in OnClick of TextView , but nothing, I do not give any error, but I do not copy it in the clipboard. Surely this is wrong,

Could someone tell me how to do it, either as a tag or as a java code?

   public class FragLay extends Activity {
    TextView textView3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frag_lay);
        textView3=(TextView)findViewById(R.id.textView);
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager myClipboard = myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                ClipData myClip;
                int min = 0;
                int max = textView3.getText().length();
                if (textView3.isFocused()) {
                    final int selStart = textView3.getSelectionStart();
                    final int selEnd = textView3.getSelectionEnd();
                    min = Math.max(0, Math.min(selStart, selEnd));
                    max = Math.max(0, Math.max(selStart, selEnd));
                    final CharSequence selectedText = textView3.getText().subSequence(min, max);
                    String text = selectedText.toString();
                    myClip = ClipData.newPlainText("text", text);
                    myClipboard.setPrimaryClip(myClip);
                }
            }
        });

    }


}
    
asked by Ignacio Belmonte 10.04.2018 в 16:13
source

2 answers

2

Try that at the moment of clicking, you get the text of TextView and keep it in the holder:

textView3.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            // obtenemos el texto del textView3
            String text = textView3.getText().toString();
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
            ClipData clip = ClipData.newPlainText("text",  text);
            clipboard.setPrimaryClip(clip);

            return false;
        }
});
    
answered by 10.04.2018 в 16:52
2

The problem is that you are determining to copy the data to the clipboard if the TextView is being focused, which will not happen just by clicking, I suggest you delete this part:

  if (textView3.isFocused()) {

The other problem is that you are not correctly obtaining the text within TextView , simply use:

String text = textView3.getText().toString();

this would be the corrected code:

    textView3=(TextView)findViewById(R.id.textView);
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ClipboardManager myClipboard = myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                ClipData myClip;

                String text = textView3.getText().toString();
                myClip = ClipData.newPlainText("text", text);
                myClipboard.setPrimaryClip(myClip);

            }
        });

This way you can save the text contained in TextView in the clipboard.

    
answered by 10.04.2018 в 18:14