top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Link in TextView

+2 votes
312 views

I have a TextView that has lengthy text. I want to give link only for some text in it, the link need not be URL. How can I do that? Help.

posted Aug 17, 2016 by Vijay

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

What you are looking for are SpannableString and ClickableSpan,

SpannableString spannableString = new SpannableString("I want to link something here");
ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View textView) {
        //.... Do your code here
    }
    @Override
    public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
};
spannableString.setSpan(clickableSpan, 24, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView textView = (TextView) findViewById(R.id.textview);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);

Hope this helps!

answer Aug 23, 2016 by Vinod Kumar K V
...