Links in TextView
I need to put a link in a TextView , I have a line containing the <a href="link">Text for link</a> tag and other text. The problem is that if I run the project, I can see the text, but it is not clickable. I also tried with the <b> tag to make sure this works, and it seems like it doesn't work either.
How can I get this to work without using Linkify ?
Thanks for your help.
I managed to do this work after I found some examples in android samples.
here is the code:
textView.setText(Html.fromHtml( "<b>text3:</b> Text with a " + "<a href=\"http://www.google.com\">link</a> " + "created in the Java source code using HTML.")); textView.setMovementMethod(LinkMovementMethod.getInstance()); Hope this helps others ...
Getting links working with html is quite difficult:
Apply the text via xml
android:text="@string/β¦or viasetText()(see other answers)Use
textView.setMovementMethod(LinkMovementMethod.getInstance())to make links clickable (see other answers)- add
android:autoLink="web"to you XML resource (TextViewsection), otherwise A tags will not display correctly and will no longer be available. / li>
Note 1:OnClickListener can be useful if your TextView contains only one link and you want to activate navigation, even if the user clicks on your link, but inside the TextView .
Note 2:android:linksClickable="true" still doesn't work (with Android 3.2), use p instead. 2
Linkify is a class that you should use to create links. By the way, what is the reason for not using Linkify?
You can associate all text in text form with actions such as visiting a website or calling a phone number based on a pattern. Android provides the easiest way to do this. Consider the code below.
TextView noteView = (TextView) findViewById(R.id.noteview); noteView.setText(someContent); Linkify.addLinks(noteView, Linkify.ALL); The Linkify class provides different options for creating custom links. Google has posted a blog post about this .
I could not 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()); The text view looks like this:
<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 res line:
<string name="tv_terms_of_use_html"> <![CDATA[This is link to <a href="http://google.com/">Google</a>.]]> </string> The important part: Linkify.addLinks needs to be done before tvTermsOfUse.setMovementMethod , otherwise it will not work.
XML does not require other settings.
It took me about an hour to figure this out, hope this helps someone.
EDIT:
According to comment by @rfellons
Thank. Also works for me ... BUT only with
<uses-permission android:name="android.permission.INTERNET"/>at Manifest.xml. - rfellons September 7 at 13:31
Be sure to check it out.
Using
android:linksClickable="true" android:autoLink="web" For some reason I cannot answer your answer; I just wanted to say that you can omit textView.setText and just put it in a string resource and set this using android:text . You just need to save textView.setMovementMethod(LinkMovementMethod.getInstance()); ; unfortunately android:linksClickable="true" by itself does not work.
Solution: Linkify.addLinks (chatText, Linkify.ALL);
This works pretty well: (In the textview properties, inside the xml file)
android:autolink="web" To dynamically add links (selected 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 the following:
<string name="link_from_server">Dynamic Link</string> This will add a βDynamic Linkβ to your text view, and if you touch this, it will follow the link provided by your server.