user3410344
user3410344

Reputation: 41

How can I copy Section of textView not all of that in this code?

I have this code from Mr Naddy for copying textView to clipboard in API 7

TextView textView=(TextView)findViewById(R.id.textView1);
registerForContextMenu(textView);

Then override onCreateContextMenu -

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    //user has long pressed your TextView
    menu.add(0, v.getId(), 0, "Copy");

    //cast the received View to TextView so that you can get its text
    TextView textView = (TextView) v;

    //place your TextView's text in clipboard
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
    clipboard.setText(textView.getText());
}

It works ..... but the User can copy just all of textview ................. I want allow to user if he wants he can copy section of textview .... maybe user want to copy for example some words not all of textview ???????

what should I do ??

Upvotes: 0

Views: 113

Answers (1)

Matej Špilár
Matej Špilár

Reputation: 2687

have you tried this ?

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);

found at: Android: Copy to clipboard selected text from a TextView

Upvotes: 1

Related Questions