I know that if you put a link in a textview it will work but if I want to display for example:
google stackoverflow
and not the whole link(just the tag) How do i make those links clickable?
I know that if you put a link in a textview it will work but if I want to display for example:
google stackoverflow
and not the whole link(just the tag) How do i make those links clickable?
You could have two separate TextViews and you could align them accordingly in your layout if needed:
Text1.setText(
Html.fromHtml(
"<a href=\"http://www.google.com\">google</a> "));
Text1.setMovementMethod(LinkMovementMethod.getInstance());
Text2.setText(
Html.fromHtml(
"<a href=\"http://www.stackoverflow.com\">stackoverflow</a> "));
Text2.setMovementMethod(LinkMovementMethod.getInstance());
Then if you want to strip the "link underline". Create a class:
public class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
Then add this method in your main Activity class where you have the TextViews
private void stripUnderlines(TextView textView) {
Spannable s = new SpannableString(textView.getText());
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}
And then just call this after you initialised the TextViews (in your onCreate):
stripUnderlines(Text1);
stripUnderlines(Text2);
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.
android:autoLink="web"
simply works if you have full links in your HTML. The following will be highlighted in blue and clickable:
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...
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>
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"
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" />
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
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.