How can I make several clickable parts of text in TextView. Every clickable part must have his own action.
I store main text and clickable words in different resources.
Strings in resources does not exist for some configurations.
String[] links = new String[3];
links[0] = cntx.getString(cntx.getResources().getIdentifier("footerLink1", "string", cntx.getPackageName()));
links[1] = cntx.getString(cntx.getResources().getIdentifier("footerLink2", "string", cntx.getPackageName()));
links[2] = cntx.getString(cntx.getResources().getIdentifier("footerLink3", "string", cntx.getPackageName()));
String text = String.format(cntx.getString(cntx.getResources().getIdentifier("footerDisclaimer", "string", cntx.getPackageName())), links[0], links[1], links[2]);
SpannableString ss = new SpannableString(text);
setSpanOnLink(ss, links[0], new ClickableSpan() {
@Override
public void onClick(View textView) {
Log.i("Disclaimer Footer", "1 click");
//TODO run item
}
});
setSpanOnLink(ss, links[1], new ClickableSpan() {
@Override
public void onClick(View textView) {
Log.i("Disclaimer Footer", "2 click");
//TODO run item
}
});
setSpanOnLink(ss, links[2], new ClickableSpan() {
@Override
public void onClick(View textView) {
Log.i("Disclaimer Footer", "3click");
//TODO run item
}
});
TextView t1 = new TextView(cntx);
t1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 8);
t1.setText(ss);
t1.setMovementMethod(LinkMovementMethod.getInstance());
private void setSpanOnLink(SpannableString ss, String link, ClickableSpan cs) {
String text = ss.toString();
int start = text.indexOf(link);
int end = start + link.length();
ss.setSpan(cs, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}