This is my working implementation
private void showMessage()
{
lblMessage.setText("");
List<String> messages = db.getAllGCMMessages();
for (int k = messages.size() - 1; k >= 0; --k)
{
String message = messages.get(k).toString();
lblMessage.append(message + "\n\n");
}
Linkify.addLinks(lblMessage, Linkify.ALL);
}
and to change color of hyperlinks , i editted my xml for textview -
android:textColorLink="#69463d"
this should work.
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
and
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/txtCredits"
android:id="@+id/text2"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"></TextView>
What about data binding?
@JvmStatic
@BindingAdapter("textHtml")
fun setHtml(textView: TextView, resource: String) {
val html: Spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(resource, Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml(resource)
}
textView.movementMethod = LinkMovementMethod.getInstance()
textView.text = html
}
strings.xml
<string name="text_with_link"><a href=%2$s>%1$s</a> </string>
in your layout.xml
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:textHtml="@{@string/text_with_link(model.title, model.url)}"
tools:text="Some text" />
Where title and link in xml is a simple String
Also you can pass multiple arguments to data binding adapter
@JvmStatic
@BindingAdapter(value = ["textLink", "link"], requireAll = true)
fun setHtml(textView: TextView, textLink: String?, link: String?) {
val resource = String.format(textView.context.getString(R.string.text_with_link, textLink, link))
val html: Spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(resource, Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml(resource)
}
textView.movementMethod = LinkMovementMethod.getInstance()
textView.text = html
}
and in .xml pass arguments separately
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:link="@{model.url}"
app:textLink="@{model.title}"
tools:text="Some text" />
android:autoLink="web"
simply works if you have full links in your HTML. The following will be highlighted in blue and clickable:
Use
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="www.google.com" />
This flag
autolink="web"
controls whether links such as urls automatically found and converted to clickable links. The default value is "none", disabling this feature. Values: all, email, map, none, phone, web.
I hit on the same problem and finally find the working solution.
in the string.xml file, define:
<string name="textWithHtml">The URL link is <a href="http://www.google.com">Google</a></string>
Replace the "<" less than character with HTML escaped character.
In Java code:
String text = v.getContext().getString(R.string.textWithHtml);
textView.setText(Html.fromHtml(text));
textView.setMovementMethod(LinkMovementMethod.getInstance());
And the TextBox will correctly display the text with clickable anchor link
TextView t2 = (TextView) findViewById(R.id.textviewidname);
t2.setMovementMethod(LinkMovementMethod.getInstance());
and
<string name="google_stackoverflow"><a href="https://stackoverflow.com/questions/9852184/android-textview-hyperlink?rq=1">google stack overflow</a></string>
The link is, "Android: textview hyperlink"
and the tag is, "google stack overflow"
Define the first code block in your java and the second code block in your strings.xml file. Also, be sure to reference the id of the textView from your page layout in your java.
Very simple way to do this---
In your Activity--
TextView tv = (TextView) findViewById(R.id.site);
tv.setText(Html.fromHtml("<a href=http://www.stackoverflow.com> STACK OVERFLOW "));
tv.setMovementMethod(LinkMovementMethod.getInstance());
Then you will get just the Tag, not the whole link..
Hope it will help you...
Source: Stackoverflow.com