Reputation: 577
I have a text with words that I like to be clickable in a textview.
I found this good rated answer here
The text elements look like links in the textview but the click-function does not get triggered.
my XML:
<TextView
android:id="@+id/mainTextView"
android:layout_width="0dp"
android:layout_height="193dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:clickable="true"
android:enabled="false"
android:linksClickable="true"
android:text="@string/main_text"
app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
and here my code:
setTextViewHTML(lv_mainTextView, getResources().getString(R.string.main_text));
...
protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
{
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
@Override
public void onClick(View view) {
// Do something with span.getURL() to handle the link click...
Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_LONG).show();
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
protected void setTextViewHTML(TextView text, String html)
{
CharSequence sequence = Html.fromHtml(html);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for(URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
text.setText(strBuilder);
text.setMovementMethod(LinkMovementMethod.getInstance());
}
When I debugged it the function "makeLinkClickable" is being called but onCLick is never triggered.
Any ideas and help would be appreciated!!!
Thanks Kev
EDITED:
My text in the resource Looks like that:
"The <a href="tree">tree</a> is larger than the <a href="flower">flower</a>"
Which gives me the following string when fetching it with fromHTML:
"The <a href="tree">tree</a> is larger than the <a href="flower">flower</a>"
Upvotes: 1
Views: 106
Reputation: 1320
Your code is correct. Just remove this line :
android:enabled=false
Upvotes: 1
Reputation: 59
Maybe this will help you:
private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener) {
SpannableString link = new SpannableString(text);
link.setSpan(new ClickableString(listener), 0, text.length(),
SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
return link;
}
private void makeLinksFocusable(TextView tv) {
MovementMethod m = tv.getMovementMethod();
if ((m == null) || !(m instanceof LinkMovementMethod)) {
if (tv.getLinksClickable()) {
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
/*
* ClickableString class
*/
private static class ClickableString extends ClickableSpan {
private View.OnClickListener mListener;
public ClickableString(View.OnClickListener listener) {
mListener = listener;
}
@Override
public void onClick(View v) {
mListener.onClick(v);
}
}
Example:
private void spannableStringTEST()
{
String text = "SOME TEXT! HELLO! YEA";
String text2 = "Some other text.";
// Links
SpannableString link = makeLinkSpan("YOUR CLICKABLE TEXT", new View.OnClickListener() {
@Override
public void onClick(View v) {
// respond to click
Toast.makeText(mContext, "IT WORKS", Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(Intent.ACTION_VIEW, "http://www.google.com");
//context.startActivity(intent);
}
});
TextView text = findViewByID(R.id.tv_cardView_text);
text .setText(text);
text .append(link);
text .append(text2);
makeLinksFocusable(text);
}
My xml TextView:
<TextView
android:id="@+id/tv_cardView_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Hope you can do something with it!
Upvotes: 0