Skip to content

Commit de8946f

Browse files
author
Paul Soucy
committed
added clickable links to StringUtils
1 parent 7d7e409 commit de8946f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

devsmartlib/src/com/devsmart/android/StringUtils.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
import java.util.List;
99

1010
import android.content.res.Resources;
11+
import android.text.Spannable;
12+
import android.text.SpannableString;
13+
import android.text.Spanned;
14+
import android.text.method.LinkMovementMethod;
15+
import android.text.method.MovementMethod;
16+
import android.text.style.ClickableSpan;
17+
import android.view.View;
18+
import android.widget.TextView;
1119

1220
public class StringUtils {
1321

@@ -64,5 +72,32 @@ public static String[] intersection(String[] a, String[] b){
6472
retval.toArray(retarray);
6573
return retarray;
6674
}
75+
76+
public abstract static class ClickSpan extends ClickableSpan {
77+
78+
}
79+
public static void clickify(TextView view, final String clickableText,
80+
final ClickSpan span) {
81+
82+
CharSequence text = view.getText();
83+
String string = text.toString();
84+
85+
int start = string.indexOf(clickableText);
86+
int end = start + clickableText.length();
87+
if (start == -1) return;
88+
89+
if (text instanceof Spannable) {
90+
((Spannable)text).setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
91+
} else {
92+
SpannableString s = SpannableString.valueOf(text);
93+
s.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
94+
view.setText(s);
95+
}
96+
97+
MovementMethod m = view.getMovementMethod();
98+
if ((m == null) || !(m instanceof LinkMovementMethod)) {
99+
view.setMovementMethod(LinkMovementMethod.getInstance());
100+
}
101+
}
67102

68103
}

0 commit comments

Comments
 (0)