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.