I'm using the Html.fromHtml()
function to set the text of a TextView in multiple screens. All of them are of the following pattern: <a href="http://link.com">Name</a>
. I used the Log to see if the strings were correct, and they are.
But there's one common thing on the two cases that aren't working: i'm inflating and copying a row layout.
I'm using a LinearLayout as a row and adding multiple of these inside another linearlayout.
The row layout that is being copied:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/bandName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginTop="15dp"
android:text="Band"
android:textColor="@color/darkTxtLightBg"
android:textColorLink="@color/darkTxtLightBg"
android:clickable="true"
android:linksClickable="true"
android:textSize="17sp" />
<TextView
android:id="@+id/bandOnTour"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:gravity="end"
android:text="Band"
android:textColor="@color/darkTxtLightBg"
android:textSize="16sp" />
</LinearLayout>
The LinearLayout where i insert the rows:
<LinearLayout
android:id="@+id/table_bandList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_marginTop="10dp">
</LinearLayout>
The code where i copy, populate and add these views:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout tr;
for (Band b : bandsFollowed) {
tr = (LinearLayout) inflater.inflate(R.layout.bandrow_followedlist, null);
TextView t1 = (TextView) tr.findViewById(R.id.bandName);
t1.setText(Html.fromHtml("<a href=\"" + b.url + "\">" + b.name + "</a>"));
t1 = (TextView) tr.findViewById(R.id.bandOnTour);
t1.setText(getResources().getString(R.string.onTour));
t1.setTextColor(getResources().getColor(R.color.bandFollowed));
tl.addView(tr);
}
What i tried:
android:clickable="true"
, but it made no difference. android:onClick
attribute with a function, but that won't work as i need to. So, why does it work when i simply set a text to another TextView that i have, but it doesn't work when i add them dynamically? Could it be something with the inflater, the parent or even the xml file?
I know the link is there because the text is underlined, but nothing happens when i click it.
Try the following code, I have tested. Hope this helps!
TextView textView = (TextView) findViewById(R.id.textView);
textView.setMovementMethod(LinkMovementMethod.getInstance());
final String htmlString="<a href=\"http://www.google.com\">Go to Google</a>";
textView.setText(Html.fromHtml(htmlString));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.