SERG
SERG

Reputation: 3971

How to copy a word from TextView on tap(click)?

I`d like to copy a word to buffer from textview by tap on this word. I have done it whit the help of textView.setTextIsSelectable(true); but on slide it select the word too

is there a way to get something similiar to textView.getSelectionStart() or position of the click in textview (char number) without using textView.setTextIsSelectable(true); thanks

Upvotes: 0

Views: 362

Answers (2)

null pointer
null pointer

Reputation: 5914

On click of the textview copy the string in the textview to the clipboard

textview.setOnClickListener(new OnClickListener()
            {

            @Override
            public void onClick(View v)
                {
                ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
                ClipData clip = ClipData.newPlainText("label", "String to copy");
                clipboard.setPrimaryClip(clip);
                }
        });

Upvotes: 1

snachmsm
snachmsm

Reputation: 19243

you might use ClickableSpan for this

final String text = "your text with a lot of words";
Spannable span = Spannable.Factory.getInstance().newSpannable(text);   
span.setSpan(new ClickableSpan() {  
    @Override
    public void onClick(View v) {  
        Log.i("picked word", "this log is "+text.substring(0, 4));
    } 
}, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);

note that text styling for this word will change, you should override this

Upvotes: 1

Related Questions