0

Is it possible to allow the user to select and then copy text in the clipboard in a TextView?

I found this but there isn't an answer.

I have also tried to set android:textIsSelectable="true" but it didn't work.

Community
  • 1
  • 1
kingston
  • 11,053
  • 14
  • 62
  • 116
  • 2
    see this post maybe helpfull http://stackoverflow.com/questions/6624763/android-copy-to-clipboard-selected-text-from-a-textview – ρяσѕρєя K Apr 25 '12 at 13:36
  • Thanks, that will help but it isn't the answer. It will work when the user will be able to select a text. – kingston Apr 25 '12 at 13:42
  • use EditText instead of TextView and make it not editable? – ρяσѕρєя K Apr 25 '12 at 13:51
  • It has several problems. 1) it shows useless things like the marks for wrongly spelled words, 2) on some devices if you allow the focus to be acquired it does weird things like allowing you to paste in the edittext even is it is not editable 3) if you disallow the focus the selection is not possible anymore... – kingston Apr 25 '12 at 14:31

2 Answers2

2

I fixed it by using an EditText but to avoid the problems I described in my answer to "imran khan" I found a comment about setKeyListener in the android code:

 * Be warned that if you want a TextView with a key listener or movement
 * method not to be focusable, or if you want a TextView without a
 * key listener or movement method to be focusable, you must call
 * {@link #setFocusable} again after calling this to get the focusability
 * back the way you want it.

So the problem is that when you set the flag editable to false setKeyListener is called and the focusable flag is overwritten.

To fix it, in the onCreate of my activity I added:

    tesxtView.setKeyListener(null);
    tesxtView.setFocusable(true);

By doing this I also got rid of the marks for the wrongly spelled words

kingston
  • 11,053
  • 14
  • 62
  • 116
1

I think that starting with Lollipop, this actually works as you'd expect it (tested on my app, after I've changed a few things) :

enter image description here

I used this library, and changed this attribute for the file "adp_alert_dialog_material.xml", to just have the attribute you've asked about (on a TextView) :

android:textIsSelectable="true"
android developer
  • 114,585
  • 152
  • 739
  • 1,270