Siva Kumar
Siva Kumar

Reputation: 893

Select text from TextView?

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,

Upvotes: 13

Views: 10440

Answers (3)

Soheil Setayeshi
Soheil Setayeshi

Reputation: 2343

Use this:

android:textIsSelectable="true"

as easy as pie ;)

Edit:

setting it in code: textView.setTextIsSelectable(true)

Upvotes: 12

Ruobin Wang
Ruobin Wang

Reputation: 378

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

Upvotes: 0

CrackerJack9
CrackerJack9

Reputation: 3651

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

Upvotes: 2

Related Questions