Reputation: 5216
Can anybody please tell me how to copy the text present in a particular textview to clipboard when a button is pressed?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
textView = (TextView) findViewById(R.id.textview);
copyText = (Button) findViewById(R.id.bCopy);
copyText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String getstring = textView.getText().toString();
// Help to continue :)
}
});
}
I want to copy the Text in TextView textView to clipboard when the Button bCopy
is pressed.
Upvotes: 479
Views: 309542
Reputation: 9263
As a handy kotlin extension:
fun Context.copyToClipboard(text: CharSequence){
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("arbitrary label",text)
clipboard.setPrimaryClip(clip)
}
Update:
If you using ContextCompat you should use:
ContextCompat.getSystemService(this, ClipboardManager::class.java)
Update for Compose:
val localClipboardManager = LocalClipboardManager.current
localClipboardManager.setText(AnnotatedString("Your text here"))
Upvotes: 73
Reputation: 54672
Use ClipboardManager
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
Make sure you have imported android.content.ClipboardManager
and NOT android.text.ClipboardManager
. Latter is deprecated.
Check this link for Further information.
Upvotes: 843
Reputation: 562
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clipboardManager.setPrimaryClip(ClipData.newPlainText("text","this is copied...paste it" ))
Upvotes: 3
Reputation: 447
Safe cast (as?) approach in Kotlin:
(context.getSystemService(CLIPBOARD_SERVICE) as? ClipboardManager)?.let {
val clipData = ClipData.newPlainText("Google", "https://google.com")
it.setPrimaryClip(clipData)
}
Upvotes: 2
Reputation: 31
Simplest Code in Kotlin-
val myClipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager val myClip: ClipData = ClipData.newPlainText("Label", "text") myClipboard.setPrimaryClip(myClip)
Note: Make sure you have imported android.content.ClipboardManager and NOT android.text.ClipboardManager. Latter is deprecated.
Upvotes: 3
Reputation: 11
For Kotlin Just type this code inside your Button. This code working for Fragment class.
var myClipboard = getSystemService(requireContext(), ClipboardManager::class.java) as ClipboardManager
val copyText = quotes//quotes is actual text(I want to copy) that is assign in copyText.
val clip = ClipData.newPlainText("Copied",copyText)
myClipboard.setPrimaryClip(clip)
Toast.makeText(requireActivity(), "Copied", Toast.LENGTH_SHORT).show()
Upvotes: 1
Reputation: 1281
If you want to copy text from edittext So First Create Edittext
EditText mResultEt = findViewById(R.id.resultEt);
then Create One Button on which after clicking we can copy these text
ImageButton copyClipBoard = findViewById(R.id.btn_copy);
then use the listener of the button
Java
copyClipBoard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboardManager = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("nonsense_data",
mResultEt.getText().toString());
clipboardManager.setPrimaryClip(clipData);
Toast.makeText(MainActivity.this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
}
});
Kotlin
btn1.setOnClickListener{
val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText(
"nonsense_data",
content_et.getText().toString()
)
clipboardManager.setPrimaryClip(clipData)
Toast.makeText(this@MainActivity, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show()
}
and make sure to import this
import android.content.ClipboardManager;
don't import this one
android.text.ClipboardManager
Upvotes: 6
Reputation: 93
Simple Kotlin Extension Function for Copying Text to Clipboard
fun Context.copyToClipboard(clipLabel: String, text: CharSequence){
val clipboard = ContextCompat.getSystemService(this, ClipboardManager::class.java)
clipboard?.setPrimaryClip(ClipData.newPlainText(clipLabel, text))
toast("Copied $clipLabel")
}
Upvotes: 1
Reputation: 1669
For Jetpack Compose
val localClipboardManager = LocalClipboardManager.current
localClipboardManager.setText(AnnotatedString("Your text here"))
Upvotes: 53
Reputation: 771
==> Its too Easy to Copy Content on the click of View.
-> Store Text into String variable.
-> Make Variable of ClipboardManager
-> Make Variable of ClipData
-> must imported from the package as " android.content."
-> set clip into clipboard.setPrimaryclick.
-> done.
ex.
import android.content.ClipboardManager;
import android.content.ClipData;
stringNodetxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String stringNodeCopied= stringNodetxt.getText().toString();
ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = android.content.ClipData.newPlainText("Copied", stringNodeCopied);
clipboard.setPrimaryClip(clip);
Toast.makeText(getBaseContext(), "copied to clipboard!", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 0
Reputation: 41
Simple and Easy Android Using Java.
copytext.setOnClickListener(v -> {
ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
String Text = "Hello World!";
myClip = ClipData.newPlainText("text", Text);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(this, "Copy", Toast.LENGTH_SHORT).show();
});
Upvotes: 4
Reputation: 891
With Jetpack Compose, it's really easy:
AmbientClipboardManager.current.setText(AnnotatedString("Copied Text"))
Upvotes: 3
Reputation: 477
If you want to make it shorter use:
ClipData clip = ClipData.newPlainText(label, text);
((ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(clip);
Upvotes: 0
Reputation: 6058
fun Context.copyToClipboard(text: CharSequence){
val clipboard = ContextCompat.getSystemService(this,ClipboardManager::class.java)
clipboard?.setPrimaryClip(ClipData.newPlainText("",text))
}
Upvotes: 12
Reputation: 2670
This can be done in Kotlin like this:
var clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
var clip = ClipData.newPlainText("label", file.readText())
clipboard.setPrimaryClip = clip
Where file.readText()
is your input string.
Upvotes: 14
Reputation: 650
to search clip board list first get the clipboard object like this :
private val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
then check if there is any data in clip board by this function :
fun isClipboardContainsData() : Boolean{
return when{
!clipboard.hasPrimaryClip() -> false
else -> true
}
}
then use this function to go through the clipboard object like below:
fun searchClipboard() : ClipData.Item? {
return if (isClipboardContainsData()){
val items = clipboard.primaryClip
val clipboardSize = items?.itemCount ?: 0
for (i in 0..clipboardSize) {
val item = items?.getItemAt(i)
return if (item != null){
return item
}else
null
}
return null
}else
null
}
here you can see that the searchClipboard Item will return an item of type ClipData.Item, the clipboard contains a list of ClipData.Item and if you go through implementation of clipboard this is what you will find about ClipData.Item:
public static class Item {
final CharSequence mText;
final String mHtmlText;
final Intent mIntent;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Uri mUri;
}
so what you can hold in a clipboard item could be of type:
Upvotes: 0
Reputation: 571
For copy any text in Android:
TextView text = findViewById(R.id.text_id);
ImageView icons = findViewById(R.id.copy_icon);
icons.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("text whatever you want", text.getText().toString());
clipboardManager.setPrimaryClip(clipData);
Toast.makeText(context, "Text Copied", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 0
Reputation: 2837
In Kotlin I have an extension for this
fun Context.copyToClipboard(text: String) {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip =
ClipData.newPlainText(getString(R.string.copy_clipboard_label, getString(R.string.app_name)),text)
clipboard.setPrimaryClip(clip)
}
Upvotes: 1
Reputation: 101
ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
Upvotes: 3
Reputation: 994
You can perform this copy to clipboard function when onclick button event. so put these code lines inside your button onClickListerner
android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clipData = android.content.ClipData.newPlainText("Text Label", ViewPass.getText().toString());
clipboardManager.setPrimaryClip(clipData);
Toast.makeText(getApplicationContext(),"Copied from Clipboard!",Toast.LENGTH_SHORT).show();
Upvotes: 0
Reputation: 2709
use this method:
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
at the place of setPrimaryClip we can also use the following methods:
void clearPrimaryClip()
Clears any current primary clip on the clipboard.
ClipData getPrimaryClip()
Returns the current primary clip on the clipboard.
ClipDescription getPrimaryClipDescription()
Returns a description of the current primary clip on the clipboard but not a copy of its data.
CharSequence getText()
This method is deprecated. Use getPrimaryClip() instead. This retrieves the primary clip and tries to coerce it to a string.
boolean hasPrimaryClip()
Returns true if there is currently a primary clip on the clipboard.
Upvotes: 4
Reputation: 45042
Put this method somewhere in Util class. This method attach click listener on textview to Copy Content of textView to a clipText on click of that textView
/**
* Param: cliplabel, textview, context
*/
fun attachClickToCopyText(textView: TextView?, clipLabel: String, context: Context?) {
if (textView != null && null != context) {
textView.setOnClickListener {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(clipLabel, textView!!.text)
clipboard.primaryClip = clip
Snackbar.make(textView,
"Copied $clipLabel", Snackbar.LENGTH_LONG).show()
}
}
}
Upvotes: 0
Reputation: 1
Try the following code. It will support the latest API:
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard.hasPrimaryClip()) {
android.content.ClipDescription description = clipboard.getPrimaryClipDescription();
android.content.ClipData data = clipboard.getPrimaryClip();
if (data != null && description != null && description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
{
String url= (String) clipboard.getText();
searchText.setText(url);
System.out.println("data="+data+"description="+description+"url="+url);
}}
Upvotes: 0
Reputation: 11244
String stringYouExtracted = referraltxt.getText().toString();
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", stringYouExtracted);
clipboard.setPrimaryClip(clip);
Toast.makeText(getActivity(), "Copy coupon code copied to clickboard!", Toast.LENGTH_SHORT).show();
Upvotes: 2
Reputation: 2311
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) DetailView.this
.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText("" + yourMessage.toString());
Toast.makeText(AppCstVar.getAppContext(),
"" + getResources().getString(R.string.txt_copiedtoclipboard),
Toast.LENGTH_SHORT).show();
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) DetailView.this
.getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData
.newPlainText("message", "" + yourMessage.toString());
clipboard.setPrimaryClip(clip);
Toast.makeText(AppCstVar.getAppContext(),
"" + getResources().getString(R.string.txt_copiedtoclipboard),
Toast.LENGTH_SHORT).show();
}
Upvotes: 2
Reputation: 6791
Here the method to copy text to clipboard:
private void setClipboard(Context context, String text) {
if(android.os.Build.VERSION.SDK_INT < 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("Copied Text", text);
clipboard.setPrimaryClip(clip);
}
}
This method is working on all android devices.
Upvotes: 93
Reputation: 497
use this function for copy to clipboard
public void copyToClipboard(String copyText) {
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(copyText);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData
.newPlainText("Your OTP", copyText);
clipboard.setPrimaryClip(clip);
}
Toast toast = Toast.makeText(getApplicationContext(),
"Your OTP is copied", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 50, 50);
toast.show();
//displayAlert("Your OTP is copied");
}
Upvotes: 7
Reputation: 366
use this code
private ClipboardManager myClipboard;
private ClipData myClip;
TextView textView;
Button copyText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
textView = (TextView) findViewById(R.id.textview);
copyText = (Button) findViewById(R.id.bCopy);
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
copyText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = textView.getText().toString();
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Text Copied",
Toast.LENGTH_SHORT).show();
}
});
}
Upvotes: 8
Reputation: 998
Just use this. It works only for android api >= 11 before that you'll have to use a ClipData.
ClipboardManager _clipboard = (ClipboardManager) _activity.getSystemService(Context.CLIPBOARD_SERVICE);
_clipboard.setText(YOUR TEXT);
Hope it helped you :)
[UPDATE 3/19/2015]
Just like Ujjwal Singh said it the method setText
is deprecated now, you should use, just as the docs recommande it, setPrimaryClip(clipData)
Upvotes: 15