11

I need to highlight and make url in the text clickable, dynamically.

For that, I am using the below method

private SpannableString addClickablePart(String string) {


        string = string.replaceAll("\\n"," \n ");

        string += " ";
        SpannableString ss = new SpannableString(string);
        String[] words = string.split(" ");
        for (final String word : words) {


            if (CommonUtilities.isValidURL(word)) {


                int lastIndex = 0;

                while(lastIndex != -1){

                    lastIndex = string.indexOf(word+" ",lastIndex);

                    if(lastIndex != -1){
                        ClickableSpan clickableSpan = new ClickableSpan() {
                            @Override
                            public void onClick(View textView) {
                                //use word here to make a decision

                                isRefreshingNecessary = false;

                                Intent mIntent = new Intent(ctx, UserWebsiteActivity.class);
                                mIntent.putExtra("website", word);
                                startActivity(mIntent);
                            }
                        };

                        ss.setSpan(clickableSpan, lastIndex, lastIndex + word.length(),
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                        lastIndex += word.length();
                    }
                }


            }
        }

        return ss;
    }

Its working for most of the cases. But, not working for all the cases as the below example.

The pricing information provided to you in your plan terms and conditions about these number ranges will no longer apply and will be replaced by this charging structure. See www.ee.co.uk/ukcalling for further information.

As, for the above case, when I split the whole string using

 String[] words = string.split(" ");

or

 String[] words = string.split("\\s+");

I got See www.ee.co.uk/ukcalling for as a single word. Instead, I need these 3 - See,www.ee.co.uk/ukcalling and for as 3 different words, not to be as grouped as a single word.

I am unable to understand whats wrong in the way of splitting with space. Please help me to know.

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
  • 2
    Why downvoting? I mentioned what I tried. Please let me know whats wrong, so that, I can improve, instead of just downvoting. – Narendra Singh Feb 25 '16 at 11:30
  • you can also assign them to 3 different textviews for simplicity – Vivek Mishra Feb 25 '16 at 11:31
  • I have dynamic strings so, I cannot deal them in static way. I will have only single textview, in which need to highlight the url parts, wherever exists @VivekMishra – Narendra Singh Feb 25 '16 at 11:36
  • The **most likely** reason for the downvote is because it's not reproducible... Can you reproduce the issue in ideone.com and share the link? – Codebender Feb 25 '16 at 11:40
  • let me check that link, please. – Narendra Singh Feb 25 '16 at 11:41
  • How does this work? Do you get any true here for that string? CommonUtilities.isValidURL(word) – sgpalit Feb 25 '16 at 11:46
  • yes, exactly. @sgpalit. But, I checked, the problem is in split part. – Narendra Singh Feb 25 '16 at 11:48
  • string = string.replaceAll("\\n"," \n "); instead of this can you try string = string.replaceAll("\\n"," \\n "); does this matter? – sgpalit Feb 25 '16 at 12:18
  • not sure, but let me check if it works @sgpalit – Narendra Singh Feb 25 '16 at 12:41
  • @Codebender I tried the same code with same text at the link you mentioned - http://ideone.com/hbmrCb. But, strangely, its working fine at this link. But, the same thing is not working in my code. I tested using System.out.println(word); In my code, I am getting like - **See www.ee.co.uk/ukcalling for** but seems okay in the link. What may be the reason? – Narendra Singh Feb 25 '16 at 12:46
  • Do you have any not visible html characters like   between these words? – sgpalit Feb 25 '16 at 12:55
  • @sgpalit don't know, I am getting this from server and it shows space, and also its showing me space in my log – Narendra Singh Feb 25 '16 at 13:12

2 Answers2

4

Replace all non visible white space characters.

 string = string.replaceAll("\\t", " ");
 string = string.replaceAll("\\xA0", " ");
 string = string.replaceAll("\\u1680", " ");
 string = string.replaceAll("\\u180e", " ");
 string = string.replaceAll("\\u2000", " ");
 string = string.replaceAll("\\u200a", " ");
 string = string.replaceAll("\\u202f", " ");
 string = string.replaceAll("\\u205f", " ");
 string = string.replaceAll("\\u3000", " ");

for java 8

string = string.replaceAll("(^\\h*)|(\\h*$)","");

check this how-to-trim-no-break-space-in-java

Community
  • 1
  • 1
sgpalit
  • 2,676
  • 2
  • 16
  • 22
3

Try like this. it works in all cases For me

 SpannableString ss = new SpannableString("Your String");
  ClickableSpan clickableSpan = new ClickableSpan() {
  @Override
   public void onClick(View textView) {
    //Perform Click Here
   }
@Override
public void updateDrawState(TextPaint ds) {
    super.updateDrawState(ds);
    ds.setUnderlineText(false);
}
};

ss.setSpan(clickableSpan, pos, last+5, Spanned.SPAN_USER);


    desc.setText(ss);
    desc.setMovementMethod(LinkMovementMethod.getInstance());
    desc.setHighlightColor(Color.TRANSPARENT);

Please Note desc is my TextView to whom i set String. Thanks.Hope It Helps

Jagjit Singh
  • 1,909
  • 1
  • 14
  • 19