How to copy text programmatically in my Android app?
Use ClipboardManager#setPrimaryClip
method:
import android.content.ClipboardManager;
// ...
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", "Text to copy");
clipboard.setPrimaryClip(clip);
ClipboardManager
API reference
How to Copy Text to Clip Board in Android?
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.
how to copy a text to the Clipboard in android
Use ClipboardManager
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("text to clip");
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("text label","text to clip");
clipboard.setPrimaryClip(clip);
}
Button to copy the value of a string to the clipboard
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 further Information check out this link
Copy Text of a Text Field
Honeycomb deprecated android.text.ClipboardManager
and introduced android.content.ClipboardManager
.
You should check whether android.os.Build.VERSION.SDK_INT
is at least android.os.Build.VERSION_CODES.HONEYCOMB
and therefore use one or the other.
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
// Old clibpoard
android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText("the text");
}
else
{
android.content.ClipboardManager clipboard = (android.content.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clipData = android.content.ClipData.newPlainText("PlainText", "the text");
clipboard.setPrimaryClip(clipData);
}
Select and copy text in a TextView Android
Ok so, I found the problem by myself, I wasn't able to select because i have a
setMovementMethod(new ScrollingMovementMethod());
I tried to delete this one and it's work.
Thx for ur help
Copy text from TextView on Android
I think I have a solution.
Just callregisterForContextMenu(yourTextView);
and your TextView
will be registered for receiving context menu events.
Then override onCreateContextMenu
in your Activity
:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
//user has long pressed your TextView
menu.add(0, v.getId(), 0, "text that you want to show in the context menu - I use simply Copy");
//cast the received View to TextView so that you can get its text
TextView yourTextView = (TextView) v;
//place your TextView's text in clipboard
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(yourTextView.getText());
}
Hope this helps you and anyone else looking for a way to copy text from a TextView
Related Topics
How to Place Fragment Arrylist Scrollabletabsactivity
Get Text from Web Page to String
Force an Android Activity to Always Use Landscape Mode
Create a Ninepatch/Ninepatchdrawable in Runtime
How to Detect When Phone Is Answered or Rejected
Creating an Android Trial Application That Expires After a Fixed Time Period
Embedding Ads Within Recyclerview
Programmatically Check Play Store for App Updates
Android Create Folders in Internal Memory
How to Set Entire Application in Portrait Mode Only
Android Log.V(), Log.D(), Log.I(), Log.W(), Log.E() - When to Use Each One
How to Autoplay HTML5 Mp4 Video on Android
Set Inputtype for an Edittext Programmatically
How to Make a Gridlayout Fit Screen Size
What Proguard Configuration Do I Need for Firebase on Android
Create Blurry Transparent Background Effect
Locationsettingsrequest Dialog to Enable Gps - Onactivityresult() Skipped