I'm already using android:autoLink
to make links clickable in TextView
, but the problem is that i need to check if the link starts with http
or www
because if the text is, for example, "some sentence.Error happens" it set the text as clickable and I want to avoid that. Any thought?
---------------- Here is the solution: ----------------
private void setUrlClickable(TextView textViewContent, Spanned text) {
SpannableString spannableString = new SpannableString(text);
final Matcher matcher = Pattern.compile("(http|www)\\S*").matcher(spannableString);
while (matcher.find()) {
spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.list_section)), matcher.start(), matcher.end(), 0);
spannableString.setSpan(new TextClickable(matcher.group(0)), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textViewContent.setText(spannableString);
textViewContent.setMovementMethod(LinkMovementMethod.getInstance());
}