0

I am working on an application where I am receiving comments which I have to show in a list view. In the comments I am having few names of the tagged friends. The comment string generated at run-time containing few names.

Now, i have to make only those names in the complete comment string as clickable. How can I separate the Name from the complete string to add the click listener on the names.

Please suggest.

Sam-In-TechValens
  • 2,501
  • 4
  • 34
  • 67

1 Answers1

0

you can achieve this with SpannableString and ClickableSpan.

For this you need the indices of the substring you want to make clickable (sadly i haven't yet figured out an efficient way of how to get the indices right). You can even set multiple clickableSpans to one String.

Try something like this:

SpannableString text;
ClickableSpan clickMe;

    public void onCreate(...){
       text = new SpannableString(yourText);
       clickMe = new ClickableSpan(){
           @Override
           public void onClick(View textView){
              // Your ClickEvent
           }
       };
       text.setSpan(clickMe, startIndexOfSubstring, EndIndexOfSubstring, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

Then you just need to apply that text to your TextView or Item in your ListView and it will be clickable. I sadly have no experience with this Method in ListViews, but in TextViews this worked fine for me. You have to set this too:

yourTextView.setMovementMethod(LinkMovementMethod.getInstance());

Hope this helped you somehow!

walkslowly
  • 417
  • 1
  • 4
  • 16