13

Is it possible to select text from a TextView(including highlight) ?

It is possible with EditText but i need that to be done using TextView ..

Any help regarding this please ?

Thanks,

Siva Kumar
  • 893
  • 4
  • 14
  • 28
  • please elaborate ur question or display any screenshot – Pinki Feb 14 '11 at 10:49
  • Check this one : http://stackoverflow.com/questions/4874266/what-text-selection-control-is-this/4874566#4874566 – Vikas Patidar Feb 14 '11 at 10:50
  • @User333 I want to select text(example: A word) from bulk of text present in the textview.. – Siva Kumar Feb 14 '11 at 11:26
  • 2
    @vikas TextSelection is introduced in Android 2.3 ,but what if i want to use it for the older versions ? – Siva Kumar Feb 14 '11 at 11:27
  • [possible duplicate](http://stackoverflow.com/questions/4989545/make-edittext-behave-as-a-textview-in-code) from the same user – dave.c Feb 14 '11 at 12:42
  • @dave.c, they're not duplicates. That question is about how to get a very specific control (and it's only available in 2.3). This is a more general question about how to get text selection in a TextView. An answer to that question isn't an answer to this question. – James Moore Jul 01 '11 at 16:44

3 Answers3

12

Use this:

android:textIsSelectable="true"

as easy as pie ;)

Edit:

setting it in code: textView.setTextIsSelectable(true)

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Soheil Setayeshi
  • 2,343
  • 2
  • 31
  • 43
2

Not sure exactly what you are going for, and this may not be exactly what you want, but you can register it for a ContextMenu and then use onCreateContextMenu and the 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;
}
CrackerJack9
  • 3,650
  • 1
  • 27
  • 48
0

Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false.

My answer is here, hope it may help you:

https://stackoverflow.com/a/11026292/966405

Community
  • 1
  • 1
Ruobin Wang
  • 358
  • 3
  • 9