Adding a clickable web link within Android TextView - android

I am trying to add a link to a website within the text of my TextView.
I'm using Html.fromHtml() method to set the text, and the link is looking as expected, but when I click it, it's not doing anything. I tried setting movementMethod to LinkMovementMethod.getInstance(), but this just gets rid of my formatting, still not working.
agreementText.text = HtmlCompat.fromHtml("I'm familiar with terms of use of this website.", HtmlCompat.FROM_HTML_MODE_LEGACY)
agreementText.movementMethod = LinkMovementMethod.getInstance()
My TextView:
<TextView
android:id="#+id/agreement_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:linksClickable="true"
android:autoLink="all"/>

I actually found the answer in a comment to this post: Android: textview hyperlink
The comment was: "the most correct answer would be to type the HTML anchor tag directly in strings.xml, make sure the autolink property for the TextView is not set, then use setMovementMethod to set the LinkMovementMethod". So if I don't set autoLink or linksClickable, and I set LinkMovementMethod, it's working properly.

Related

How set TextView text both clickable and selectable?

My textview load html text that contains links (to website, to e-mail address ...)
tv = (TextView)((Activity)mContext).findViewById(R.id.entry_webview);
tv.setText(Html.fromHtml(myPage));
I settv.setMovementMethod(LinkMovementMethod.getInstance()); to make the linke be clickable.
I set tv.setTextIsSelectable(true); to make the text selectable.
What happens? TextView applies just the last setting, in this order case, text will be ONLY selectable and the link(s) won't be clickable, one setting excludes the other other one.
If I set in TextView in XML
android:autoLink="all"
android:textIsSelectable="true"
links don't work (e-mail yes).
Is there a way to make the text both clickable and selectable?
Thanks.
I had the same issue - this solution works for me:
The XML TextView should not have any link/selectable attributes:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Then, set everything programmatically respecting the following order:
textView.setText(Html.fromHtml(myHtml));
Linkify.addLinks(textView, Linkify.WEB_URLS);
textView.setTextIsSelectable(true); // API-11 and above
textView.setMovementMethod(LinkMovementMethod.getInstance());
have you tried using something like this tv.setText(Html.fromHtml("<a href='url'>link</a>"));

Android - Hyperlink missing underline

I use linkify to make a textview work as hyperlink, and it does work nicely. The only issue is the underline is missing, could anyone point me out what could cause the problem? shouldn't the underline come by default?
Thanks!
Take a look at the Spannable params
addLinks(Spannable text,...)
linkify class
SpannableStringBuilder class
-replying to comment-
SpannableStringbuilder implements CharSequence, which can be used in TextView.setText();
So once you finish making your underlined text, you can use TextView.setText() and still use the method your are using.
Or refer to this: How to set underline text on textview?
You can use the xml attribute autoLink="web" for the TextView widget to automatically detect if the content is a web address. Here is an example:
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textColorLink="#color/hyperlink_blue"
android:autoLink="web"
android:textSize="16sp"/>
The attribute textColor is for text other than hyperlinks, which will be 'black' in the above example and the attribute textColorLink is for any text that takes a form of a hyperlink - which will be blue per above.
You can also append other autoLink values by 'piping' them together:
android:autoLink="web|email|map|phone"
This works for TextView, AppCompatTextView (SupportV7/AppCompat), AppCompatTextView (androidx/AppCompat)

TextView Html formatting

I have a textview which should render its content with HTML formatting. From code, this can be achieved like this,
textView.setText(Html.fromHtml(someText));
Is there a way to do this through the XML?
I ask because i do not want to set the text through the code, In my case, the text comes from a DB call and displayed through an adapter.
Yes there are a bunch of (simple) tags that are understood by TextView -- if the text is set in XML from a string resource.
So basically
<TextView text="#string/foo" .. />
It is also possible to give templates like "Hello <b>%s</b>", but here you still need to run some code to fill in the value for %s
Have a look at http://developer.android.com/guide/topics/resources/string-resource.html for formatting hints and short examples.

I'd like to define an Android TextView in XML which contains text which is a link to a URL

I'd like to define a TextView in XML which contains text which is a link to a URL. Essentially the semantics of the anchor HTML tag: <a>
I have been able to do most of what I want with, the android:autoLink="web" attribute to TextView. However, the text has to contain the URL and it is displayed to the user. I'd like to display different text which links to the URL.
Example: I can say,
<TextView android:text="http://foo.com" android:autoLink="web">
But then the user sees, "http://foo.com". I'd rather say something else, like "bar".
I tried defining the string in a string resource and using a link, such as
<string name="test">bar</string>
The string shows up properly formatted, but you cannot click on it.
If you're using HTML in strings.xml for your TextView, don't use autoLink.
Instead, in your onCreate, set the movement method to use links:
TextView msg = (TextView) findViewById(R.id.text);
msg.setMovementMethod(LinkMovementMethod.getInstance());
I think autoLink is for when you're dynamically setting the text.

Links in TextView

I need to put a link in a TextView, I have a string that contains the tag Text for link and some other text.
The problem is that if I run the project I can see the text but it's not clickable. I tried with the <b> tag too to see if that works and it seems that it doesn't work too.
How can I make this to work without the Linkify usage?
Thank you for your help all.
I have managed to make this work, after I have found some examples in the android samples.
here is the code:
textView.setText(Html.fromHtml(
"<b>text3:</b> Text with a " +
"link " +
"created in the Java source code using HTML."));
textView.setMovementMethod(LinkMovementMethod.getInstance());
Hope this help others...
Getting links working from html is kind of tricky:
Apply your text via xml android:text="#string/… or via setText() (see other answers)
Use textView.setMovementMethod(LinkMovementMethod.getInstance()) to make links clickable (see other answers)
Do NOT add android:autoLink="web" to you XML resource (section TextView), otherwise A-tags are not rendered correctly and are not clickable any longer.
Remark 1:
The OnClickListener can be helpful, if your TextView contains only one link and you want to trigger the navigation even if the user clicks aside your link, but inside the TextView.
Remark 2:
android:linksClickable="true" still does not work (as of Android 3.2), use p. 2 instead
Linkify is the class you must use to create links. BTW, what is the reason for not using Linkify?
You can linkify all text in your textview for actions like visiting a website or calling a phone number based on the schema. Android provides the easiest way to do it. Consider the below code.
TextView noteView = (TextView) findViewById(R.id.noteview);
noteView.setText(someContent);
Linkify.addLinks(noteView, Linkify.ALL);
For creating custom links, the same Linkify class provides various options.
Google has published a blogpost on this .
I couldn't figure it out, but finally it started working when I did something like:
tvTermsOfUse.setText(Html.fromHtml(getString(R.string.tv_terms_of_use_html)));
Linkify.addLinks(tvTermsOfUse, Linkify.ALL);
tvTermsOfUse.setMovementMethod(LinkMovementMethod.getInstance());
Text view looks like:
<TextView
android:id="#+id/tv_terms_of_use"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textAlignment="gravity"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="15sp" />
and string res:
<string name="tv_terms_of_use_html">
<![CDATA[This is link to Google.]]>
</string>
Important part:
The Linkify.addLinks has to be done before tvTermsOfUse.setMovementMethod, otherwise it won't work.
No other settings are necessary in XML.
It took me around hour to figure it out myself, hope it helps someone.
EDIT:
According to #rfellons comment
Thanks. Also for me works ... BUT only with
<uses-permission android:name="android.permission.INTERNET"/>
on Manifest.xml. –
rfellons Sep 7 at 13:31
Make sure you check it as well.
Use
android:linksClickable="true"
android:autoLink="web"
I can't reply to your answer for some reason; I just wanted to say that you can omit the textView.setText and just put it in a string resource, and set that using android:text. You just need to keep the textView.setMovementMethod(LinkMovementMethod.getInstance());; unfortunately android:linksClickable="true" by itself does not work.
To add the links dynamically (fetched from the server), this works:
textView.setText(Html.fromHtml(
"<a href=" + response.getLink()
+ ">" + context.getString(R.string.link_from_server) + "</a> "));
and in XML add this:
android:linksClickable="true"
If your strings.xml has this:
<string name="link_from_server">Dynamic Link</string>
This will add "Dynamic Link" to your text view and if you touch on this, it will go the link provided by your server.
The Solution : Linkify.addLinks(chatText,Linkify.ALL);
This works pretty correcty:(In textview properties,inside xml file)
android:autolink="web"

Categories

Resources