简体   繁体   中英

Copy text to clipboard from TextView

The purpose is to add functionality to a copy button on teh screen itself. What it does is it copies the text9 at that pint of time ) in the textview and copies that to the user's clipboard and hence making it available to forward that text and use it in other applications. What do I do?

#Java File.
package com.dreamgoogle.gihf;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class Quotes extends Activity {

    ImageButton next;
    ImageButton previous;
    ImageButton copytext;
    TextView q;
    TextView nm;
    String[] str;
    int i, s;

... 
.... 
.... 
... 
... 
... 





        copytext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {










            }
        });
    }

q is the textview of whose text is to be copied.

clipBorad manager available, but for api 11 onwards . no options i believe, for pre 11 devices . read here . additionally search for more examples.

Use ClipBoardManager's setText method:

copytext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {



//
    q = (TextView) findViewById(R.id.txt); // fetch the textview from the layout
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
         ClipData clip = ClipData.newPlainText("label", q.getText().toString());
         clipboard.setPrimaryClip(clip);






            }
        });

Original Q / A

for only copy text put this code in your copytext.onclicklist.. method:

 ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
 ClipData clip = ClipData.newPlainText("label", ""+edittext.getText().toString());
 clipboard.setPrimaryClip(clip);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM