13

I've got a TextView that I would like to allow the user to select a range of text from within it. The TextView takes up the entire width and height of the device (minus some padding and a title at the top). In an EditText if you long-click you get a selection overlay that allows you to set your selection left and right bounds. I'd like this functionality in a TextView. I've read that in API level 9 (2.3) (http://developer.android.com/sdk/android-2.3.html) there are new text selection controls, but I'm having difficulty implementing this. I'm doing this right now:

eic = new InputConnection( bookTextView );
eic.beginBatchEdit();

But it doesn't do anything noticable. Does anyone know how to use InputConnection correctly? Thanks.

Edit: I don't necessarily need to use what I was attempting above. I ultimately want to use either a TextView or an EditText which looks and feels like a TextView and be able to select text using a dragging cursor. Then I would like to manipulate the selected text with various context menu options (or a menu that pops up above the selected text).

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131

6 Answers6

19

Here is an idea.. Add an EditText with a TextView background, Here is an example

<EditText 
android:text=" This is not an editable EditText" 
android:id="@+id/EditText01" 
android:layout_width="wrap_content" 
android:textColor = "@android:color/white"
android:editable = "false"
android:cursorVisible="false"
android:layout_height="wrap_content"
android:background = "@android:drawable/dark_header">
</EditText>

add this to your xml in the place of TextView

ngesh
  • 13,398
  • 4
  • 44
  • 60
3

You can enable the TextView's Spannable storage. See Highlight Text in TextView or WebView for an example.

See also:

http://developer.android.com/reference/android/text/Spanned.html

Community
  • 1
  • 1
Phil
  • 35,852
  • 23
  • 123
  • 164
  • 1
    I know how to use Spans and use highlights, but what I'm after is the EditText's "look and feel" when selecting text (with anchors on either side). – Jason Robinson Aug 15 '11 at 19:11
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 idea is the same as sandy's.

My code is here, hope it may help you:

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

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

After long internet surfing to find a solution, i prefered create my own class

https://github.com/orionsource/SelectableTextViewer

enter image description here

Goal features:

  1. Easy to use - only one class
  2. Support for text and Html.fromHtml
  3. Can be in ScrollView with correct touches
  4. Cursors can be redefined
  5. Color of selection can be redefined
Dmitry Nelepov
  • 7,246
  • 8
  • 53
  • 74
0

All the above solutions either too long or not working for me.

What you need is to add just textView.setTextIsSelectable(true) in your activity or fragment or adapter.

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
0

You could display the text in a WebView and enable text selection. If you want to only use a textview/edittext, here is an answer that might help you and here is information on the Spannable class that might help you accomplish what you want.

Community
  • 1
  • 1
A. Abiri
  • 10,750
  • 4
  • 30
  • 31