Reputation: 21
Need to select the text from text view to desired range and do copy.I tried using onClick Listener on Text view and also i added android:textIsSelectable="true"
at xml
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.textview1);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setKeyListener(null);
textView.setFocusable(true);
String stringYouExtracted = textView.getText().toString();
int startIndex = textView.getSelectionStart();
int endIndex = textView.getSelectionEnd();
stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);
}
});
Toast.makeText(this, "Text clipped!", Toast.LENGTH_SHORT)
.show();
}
}
But it is not selecting the text and not getting the text to be copied
Upvotes: 2
Views: 2348
Reputation: 366
you can do it this way:
ClipboardManager myClipboard = myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
EditText editText = (EditText) findViewById(R.id.editText3);
int min = 0;
int max = editText.getText().length();
if (editText.isFocused()) {
final int selStart = editText.getSelectionStart();
final int selEnd = editText.getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
// here is your selected text
final CharSequence selectedText = editText.getText().subSequence(min, max);
String text = selectedText.toString();
// copy to clipboard
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
Replace EditText with TextView.
Upvotes: 0
Reputation: 41
Try it :
txt=(TextView)findViewById(R.id.textView1);
String stringYouExtracted = txt.getText().toString();
int startIndex = txt.getSelectionStart();
int endIndex = txt.getSelectionEnd();
stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);
And Add android:textIsSelectable="true" too ...........
Upvotes: 1
Reputation: 24853
Try this..
Remove below lines..
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);
And add the below lines..
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);
}
I hope this will help..
Upvotes: 1