Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow the user to select a text range in a TextView (Similar to EditText)

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

like image 515
Jason Robinson Avatar asked Jun 10 '11 16:06

Jason Robinson


People also ask

Which method is used to set the text in a TextView?

SetText(String, TextView+BufferType) Sets the text to be displayed using a string resource identifier.

Which of the following method is used to set the EditText text?

In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file.

What is the difference between edit text and text TextView android?

EditText is used for user input. TextView is used to display text and is not editable by the user. TextView can be updated programatically at any time.

How do I copy text from TextView?

Select + copy text in a TextView? Generally, we can copy / paste the value from EditText by long click. It is an in-build functionality for Android. Now, let's say you have the same requirement in TextView .


1 Answers

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

like image 116
ngesh Avatar answered Oct 09 '22 23:10

ngesh