1

I need to have a text that will be shown to the user, and it will be possible to select a part of it and copy it to the clipboard. But without the keyboard open. I know that on api 11 and newer i can just use

text.setTextIsSelectable (true);

But what is the solution to lower OS versions?

Tzahie Leibovich
  • 131
  • 3
  • 10

2 Answers2

1

Use ContextMenu and CLIPBOARD_SERVICE:

private TextView mTextView;

protected final void onCreate(Bundle savedInstanceState) {
...
registerForContextMenu(mTextView);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
        TextView textView = (TextView) view;
        menu.setHeaderTitle(textView.getText()).add(0, 0, 0, R.string.menu_copy_to_clipboard);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    ((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)).setText(mTextView.getText());
    return true;
}
Gaurav Arora
  • 17,124
  • 5
  • 33
  • 44
  • 1
    Is there a way to allow the user to select part of the text? and not all of the text, as you suggested in your solution. So I allow him to experience normal use of copy text. – Tzahie Leibovich Apr 23 '13 at 14:09
0

After a long and time consuming search, I can't find a component that can select text in textview for android API level <=11. I have written this component that may be of help to you : new Selectable TextView in android 3 (API <=11) component

Community
  • 1
  • 1
ali shekari
  • 740
  • 1
  • 11
  • 26