Button for copying string value to clipboard

I am modifying an old Android app. I have GPS lat and long stored in a string value and displayed to the user in an editable text field when it is resolved. I want to add a button that simply takes the value of a string and copies it to the clipboard.

I looked at this: How to programmatically copy text in an Android app?

But not sure how to implement it. Any help would be wonderful, I have not touched much development in this area recently!

thanks

Edit:

//Set button (inside oncreate method) Button button = (Button)this.findViewById(R.id.buttoncopylocation); button.setOnClickListener(this); //Code added in onClick method @Override public void onClick(View arg0) { // TODO Auto-generated method stub ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("Copied", mycoords); clipboard.setPrimaryClip(clip); } 

I get this error: http://i.imgur.com/sQ4um.jpg

+4
source share
2 answers

If it is just text, it is very simple.

 ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("label","Your Text"); clipboard.setPrimaryClip(clip); 

For more information check this link.

+19
source

provide context before

 getSystemService(Context.CLIPBOARD_SERVICE); 

like

 Context context = ...; ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); 
-1
source

Source: https://habr.com/ru/post/1438388/


All Articles