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
How to select and copy text by pressing long in textView?
Try this , its will work for you :
private ClipboardManager myClipboard;
private ClipData myClip;
//inside oncreate
myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
mEditText.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
String text;
text = mEditText.getText().toString();
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Text Copied",
Toast.LENGTH_SHORT).show();
return true;
}
});
Android: Copy to clipboard selected text from a TextView
TextView tv;
String stringYouExtracted = tv.getText().toString();
int startIndex = tv.getSelectionStart();
int endIndex = tv.getSelectionEnd();
stringYouExtracted = stringYouExtracted.subString(startIndex, endIndex);
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);
EDIT (The previous is the full answer, but I ran into my answer by mistake so I would like to add):
With Newer APIs, change the last two lines to :
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", stringYouExtracted);
clipboard.setPrimaryClip(clip);
}
"Copied Text"
is a title for your COPY entity in newer APIS
How do I enable standard copy paste for a TextView in Android?
This works for copy pre-Honeycomb:
import android.text.ClipboardManager;
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(textView.getText());
Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
});
Have BOTH an onLongPress menu and select-copy text on aTextview item?
The below solution helps me for most situations.
I want text to be either selectable (for copy and paste) or want other gestures to work.
Initially setting the gestures on the text field:
- Set textIsSelectable to false, either in the layout file or programmatically.
- Set an onTouchListener on the textview with your Gestures.
- Allow one of the gestures to switch to textSelection mode. See below.
How to set textSelection programmatically?
- Set the textIsSelectable, focusable, longPressable to true
- Set the onTouchListener to null.
- Install a clickListener on the textview to allow you switch back to the original TouchListener.
1) Install your GestureHandler:
// Create your Touch Listener
onTouchListener = new OnSwipeTouchListener(mCtx, this);
view.setOnTouchListener( onTouchListener);
2) Switch to textselction modus:
// Create your popup with an menu option to switch to textselection modus:
PopupMenu popup = new PopupMenu(mCtx, view);
popup.inflate(R.menu.text_options_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case ...
case R.id.text_textisselectable:
view.setOnTouchListener(null);
((TextView)view).setTextIsSelectable( true);
((TextView)view).setFocusable( true);
((TextView)view).setLongClickable( true);
// Install a click listener to switch back to the previous Touch Listener
((TextView)view).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupx = new PopupMenu(mCtx, view);
popupx.inflate(R.menu.selecttext_back_menu);
popupx.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
((TextView)view).setTextIsSelectable( false);
((TextView)view).setFocusable( false);
((TextView)view).setLongClickable( false);
view.setOnTouchListener(onTouchListener);
return true;
}});
popupx.show();
}
});
break;
Display copy after select text from textview using swift
You need something like this, its only matters of states, if your currentState is one you should show something in the menu, you also need override each of those methods to change the current state
import UIKit
enum MenuState{
case select
case copy
case paste
}
class CustomTextField: UITextField {
var currentState : MenuState = .select
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool
{
switch self.currentState {
case .select:
if action == #selector(select(_:)) || action == #selector(selectAll(_:)){
return true
}
case .copy:
if action == #selector(copy(_:)){
return true
}
case .paste:
if action == #selector(paste(_:)){
return true
}
}
return false
}
override func select(_ sender: Any?) {
super.select(sender)
self.currentState = .copy
}
override func selectAll(_ sender: Any?) {
super.selectAll(sender)
self.currentState = .copy
}
override func copy(_ sender: Any?) {
super.copy(sender)
self.currentState = .paste
}
override func paste(_ sender: Any?) {
super.paste(sender)
self.currentState = .select
}
}
Related Topics
Accessing Contents of R.String Using a Variable to Represent the Resource Name
Retrofit - Multipart Request: Required Multipartfile Parameter 'File' Is Not Present
Gradle Failed to Resolve Library in Android Studio
Apache Commons Codec with Android: Could Not Find Method
Why Mediaplayer Throws Not Present Error When Creating Instance of It
How to Set the Divider Between Tabs in Tablayout of Design Support Library
Android - Acceleration Down (Smash)
Android: Using Simplecursoradapter to Get Data from Database to Listview
Gradle Error: Minimum Supported Gradle Version Is 3.3. Current Version Is 3.2
Passing Strings Between Activities in Android
How to Disable Instant Run for Android Studio 3.0
Lazy Load Images on Listview in Android(Beginner Level)
Login Failed Invalid Key Error with Facebook Sdk
What Is the Use of the Res/Values/Public.Xml File on Android