0

I want to copy text from TextField to Clipboard. My TextField is named txtDetails and i have a button named btnCopyToClipBoard.I want to be able to copy the contents of my textfield to clipboard when i press the button.

Name Of InputBox Is "txtDetails" Name Of Button Is "btnCopyToClipBoard"

String StrTemp  = txtDetails.getText();

So How We Can Set "OnClickListener" Of "btnCopyToClipBoard" To Copy Value In "StrTemp" To ClipBoard

Nanne
  • 64,065
  • 16
  • 119
  • 163
Hossein Kurd
  • 3,184
  • 3
  • 41
  • 71
  • possible duplicate of [how to copy a text to the Clipboard in android](http://stackoverflow.com/questions/16435151/how-to-copy-a-text-to-the-clipboard-in-android) – Zied R. Feb 27 '14 at 13:33
  • Possible duplicate of [How to copy text programmatically in my Android app?](http://stackoverflow.com/questions/238284/how-to-copy-text-programmatically-in-my-android-app) – Kristján Nov 17 '15 at 07:39

2 Answers2

5

Use ClipBoardManager's setText method:

String StrTemp  = txtDetails.getText();
btnCopyToClipBoard.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", StrTemp);
        clipboard.setPrimaryClip(clip);
    }
});
Zied R.
  • 4,964
  • 2
  • 36
  • 67
2

use global class extends Application for example:

public class G extends Application

And Call In manifiest

and use

public static Context               context;

// OnCreate
context = getApplicationContext();

And Then Change That Line As:

 ClipboardManager clipboard = (ClipboardManager) G.context.getSystemService(CLIPBOARD_SERVICE); 

Or Use

ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(CLIPBOARD_SERVICE); 
HK1988
  • 434
  • 2
  • 4
  • 21