2313
2313

Reputation: 11

Button copying text to clipboard

I am trying to copy some text into the clipboard when a button is pressed. I have found other questions like this, but i can't figure out the code. Here is the code i am trying to use now:

Button button = (Button)this.findViewById(R.id.button1);

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label","woop woop in the clipboard");
clipboard.setPrimaryClip(clip);

But i am confused on how to get the button to do the action.

Upvotes: 0

Views: 1141

Answers (5)

USER9561
USER9561

Reputation: 1084

Use OnClickListener on Button

    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label","woop woop in the clipboard");
        clipboard.setPrimaryClip(clip);

    }

Upvotes: 0

Dixit Patel
Dixit Patel

Reputation: 3192

Copy some text into the clipboard when a button is pressed

// OnClick Event

Button button = (Button) this.findViewById(R.id.video_layout);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                copyToClipboard(Activity.this,button.getText().toString())
            }
        });

// Copy to Clipboard Method

  public static boolean copyToClipboard(Context context, String text) {
            try {
                int sdk = android.os.Build.VERSION.SDK_INT;
                if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
                    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                            .getSystemService(context.CLIPBOARD_SERVICE);
                    clipboard.setText(text);
                } else {
                    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                            .getSystemService(context.CLIPBOARD_SERVICE);
                    android.content.ClipData clip = android.content.ClipData
                            .newPlainText(
                                    context.getResources().getString(
                                            R.string.message_clipboard), text);
                    clipboard.setPrimaryClip(clip);
                }
                return true;
            } catch (Exception e) {
                return false;
            }
        }

Upvotes: 0

Boe-Dev
Boe-Dev

Reputation: 1595

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("label","woop woop in the clipboard");
    clipboard.setPrimaryClip(clip);
            }
        }
    });

Upvotes: 0

Mr. 0x50
Mr. 0x50

Reputation: 314

I'n not able to test this, but try to add an OnClickListener on the Button.

Button button = (Button)this.findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {
    void onClick(View v) {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label","woop woop in the clipboard");
        clipboard.setPrimaryClip(clip);
    }
});

Upvotes: 0

Kiran Niranjan
Kiran Niranjan

Reputation: 78

Use button OnClickListener

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("label", "woop woop in the clipboard");
    clipboard.setPrimaryClip(clip);
}});

Upvotes: 2

Related Questions