On my Android application, how can I make a part of my string clickable?
You know when, for example, in Wikipedia there is a keyword colored in blue that you can click to go to another web page? I want to imitate this.
I need to make some keywords inside my string clickable, so that an user can click on them to go to another Activity.
Can you guys suggest me the best way for doing that?
Asked
Active
Viewed 1,037 times
-1

Fyd Rose
- 21
- 5
1 Answers
4
This Code can do your work.
Use SpannableString and ClickableSpane
SpannableString ss = new SpannableString("Click Here!");
ClickableSpan clickableSpan = new ClickableSpan()
{
@Override
public void onClick(View textView)
{
//do Anything you want
}
@Override
public void updateDrawState(TextPaint ds)
{
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
//6 and 11 are length of part of text you want
//In this is is Here!
ss.setSpan(clickableSpan, 6, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
Hope will Help you!!!

GameChanger
- 179
- 11