2

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

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
SARA
  • 21
  • 3

3 Answers3

1

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..

Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • It is not the problem..I need to drag the selection of the text from textview..for example if androidos is given text ..i want to copy android – SARA Oct 09 '13 at 05:52
1

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 ...........

user3410344
  • 41
  • 13
0

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.

SKG
  • 346
  • 4
  • 11