2

How to implement selecting text capability on Android 2.2? I searched Google but can't find a solution.

emeraldhieu
  • 9,380
  • 19
  • 81
  • 139
  • Could you be more specific about what views you are using and what you are trying to do? – Codeman Jul 08 '11 at 13:46
  • 1
    As I described in title, selecting text in TextView. Do you know selecting text in Notepad? it is something like that. Drag the mouse to select the text. – emeraldhieu Jul 08 '11 at 14:04
  • What Android API level are you using? Some APIs support text selection, some don't. This may also be implemented in the UI, such as HTC Sense. I don't know that writing your own method for this would be trivial. – Codeman Jul 11 '11 at 14:18
  • 1
    I tagged "android-2.2". I'm using Eclipse to write program and there is no isSelectable attrib. – emeraldhieu Jul 11 '11 at 14:30
  • I write a component to solve this problem : [http://stackoverflow.com/questions/18042308/new-selectable-textview-in-android-3-api-level-11-component][1] [1]: http://stackoverflow.com/questions/18042308/new-selectable-textview-in-android-3-api-level-11-component – ali shekari Aug 04 '13 at 11:18

5 Answers5

2

This is the only way I've found (from google) to support it for android 1.6+, it works but it's not ideal, I think in stock android you can't hold down a webview to highlight until v2.3, but I may be mistaken..

By the way this is for a webview, it might also work on textview, but I haven't tried it

(Note: this is currently what my shipped app is using, so it works with all the phones I've had it tested on, but the only reason I found this question was because I was searching and hoping that someone had come up with a better way by now)

I've just got a menu item called "Select text" which calls the function "selectnCopy()"

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //..
    switch (item.getItemId()) {
        case R.id.menu_select:
            selectnCopy();
            return true;
    //..
    }
}

Which looks like this:

public void selectnCopy() {
    Toast.makeText(WebClass.this,getString(R.string.select_help),Toast.LENGTH_SHORT).show();
    try {
        KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
        shiftPressEvent.dispatch(wv);
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

Notice I've put the select_help string there as a toast, that's just because it's not immediately clear to the user how it's supposed to work

<string name="select_help">Touch and drag to copy text.</string>
tabjsina
  • 1,582
  • 15
  • 26
  • Perfect! But I don't like the message "Text copied to clipboard" after I select the text. Any ideas to change or remove it? – emeraldhieu Jul 21 '11 at 07:28
  • 2
    Unfortunately, I don't believe there is a way of removing that toast using this method – tabjsina Jul 26 '11 at 07:42
1

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
1

Have you set the text to be selectable? like this:

android:textIsSelectable="false"  //OR true

See the Documentation for further reference.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Stefan H Singer
  • 5,469
  • 2
  • 25
  • 26
  • "error: No resource identifier found for attribute 'textIsSelectable' in package 'android'". The Properties view of TextView in Eclipse doesn't have this attribute. :-?? – emeraldhieu Jul 08 '11 at 14:03
  • 3
    It's not available until Honeycomb (API level 11) – Jerry Brady Sep 15 '11 at 21:17
  • @Stefan: It is better to add some code/text here to clarify what the link directs to, as the only link to that document should be a comment only. :) – Adil Soomro Sep 10 '12 at 10:40
1

The Link @Stefan Hållén provided only worked after API Level 11.

And Android 2.2 is API Lv.8, that's the reason why you cannot get a resource identifier.

dumbfingers
  • 7,001
  • 5
  • 54
  • 80
0

An interesting workaround:

You could try displaying your text in a webview.

You just have to write the HTML tags and all of that into your string to display, and it should be selectable using the WebKit browser.

This should be fairly lightweight and transparent to the user, and I think it would solve your problem.

Let me know if you need a code example, it should be fairly simple. Just check out the WebView docs on http://developer.android.com/resources/tutorials/views/hello-webview.html

Best of luck!

Codeman
  • 12,157
  • 10
  • 53
  • 91