androidclipboardmanager

How to add copy to clipboard button in android?


I want to add a button that will copy the text from non-editable textview inside a cardview. Can I use clipboard manager for this?


Solution

  • try this define variables

    private ClipboardManager myClipboard;
    private ClipData myClip;
    

    on button click listener

    //to copy data to clip board
        copy_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                String text;
                text = edidata.getText().toString();
    
                myClip = ClipData.newPlainText("text", text);
                myClipboard.setPrimaryClip(myClip);
    
                Toast.makeText(getApplicationContext(), "Text Copied",Toast.LENGTH_SHORT).show();
            }
        });
    

    i hope this work in your case