Based on Boy's response (and thank you for your response which helped me a lot), here is another way I implemented it without this '[' and ']' chars using an inner class to describe clickable words :
import java.util.List;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Defines a TextView widget where user can click on different words to see different actions
*
*/
public class ClickableTextView extends TextView {
public ClickableTextView(Context context) {
super(context);
}
public ClickableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ClickableTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setTextWithClickableWords(String text, List<ClickableWord> clickableWords) {
setMovementMethod(LinkMovementMethod.getInstance());
setText(addClickablePart(text, clickableWords), BufferType.SPANNABLE);
}
private SpannableStringBuilder addClickablePart(String str, List<ClickableWord> clickableWords) {
SpannableStringBuilder ssb = new SpannableStringBuilder(str);
for (ClickableWord clickableWord : clickableWords) {
int idx1 = str.indexOf(clickableWord.getWord());
int idx2 = 0;
while (idx1 != -1) {
idx2 = idx1 + clickableWord.getWord().length();
ssb.setSpan(clickableWord.getClickableSpan(), idx1, idx2, 0);
idx1 = str.indexOf(clickableWord.getWord(), idx2);
}
}
return ssb;
}
public static class ClickableWord {
private String word;
private ClickableSpan clickableSpan;
public ClickableWord(String word, ClickableSpan clickableSpan) {
this.word = word;
this.clickableSpan = clickableSpan;
}
/**
* @return the word
*/
public String getWord() {
return word;
}
/**
* @return the clickableSpan
*/
public ClickableSpan getClickableSpan() {
return clickableSpan;
}
}
}
Hope this may help someone
EDIT : how to change link color and remove underline:
Create and use your own implementation of ClickableSpan like this :
//a version of ClickableSpan without the underline
public static class NoUnderlineClickableSpan extends ClickableSpan {
private int color = -1;
public void setColor(int color) {
this.color = color;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
if (this.color != -1) {
ds.setColor(this.color);
}
}
}