I am populating textviews on the activity dynamically, based on text that is being parsed in from xml articles. The text is formatted using html tags, so I set the text like:
textViewBody.setText(Html.fromHtml(content));
The articles are set up to either some text in a body to a website, to another local article, or to simply display text. Before, we only had 1 link at the most, so I used this code:
textViewBody.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textViewBody.getUrls().length != 0) {
String link = textViewBody.getUrls()[0].getURL();
Intent i;
if (link.startsWith("article")) {
i = new Intent(getActivity().getApplicationContext(), ArticleActivity.class);
i.putExtra("file", link.substring(10));
i.putExtra("title", parentView.article.title);
startActivity(i);
} else if (link.startsWith("http")) {
i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(link));
startActivity(i);
}
} else {
// Regular body text was selected.
}
}
});
This worked perfectly, but now that some of the articles have more than one link it only opens the first one (as that's what I set it to do). My question is, how can I set the textview to know what link I have clicked on? Again, this text is dynamic (and there are around 200+ xml articles), so there's no way I can just check the contents of the string against a known list of links.
It may be important to mention that this is being done on a fragment (ArticleActivity is a tabHost, with one tab displaying the text of the article).
Don't even try to do this via onTouch. Use Spannables, and put each link inside a clickable span- http://developer.android.com/reference/android/text/style/ClickableSpan.html. Then when the text is clicked, its onClick method will be called and it can perform whatever action it needs to.