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"
Related
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.
What I would like to achieve is an effect that looks like this with a TextView:
Basically having a background, but keeping the space between the lines. The only solution I came up with was using one TextView for each line of text, but I would prefer a cleaner one using only one multiline TextView.
Any ideas?
Use spanned text for each line. Read the Spannable API.
please refer to this answer here as it describes how to implement spacing between multiple lines in a TextView , using the following properties :
android:lineSpacingMultiplier
android:lineSpacingExtra
Hope that Helps .
Regards
What you can do is use mark tag in html to textview.
myTextView.setText(Html.fromHtml("<mark>heading highlighted</mark>"));
I've started learning coding for the android, I know the basics of programming in general and thought that android would be fun, Which it has so far.
Now in my exercises in the book I have, It says to add more text to the application. The application is nothing at the moment but 1 string, And I have to add another string.
Now when I have added the string to the strings.xml file and then on the main.xml I type:
android:text="#string/AppName" />
AppName is the new string I made which in the strings.xml it looks like this:
This App is called Droid1
The weird thing is when I type in the main xml to referr to the string, It doesnt even get colour coded when I type the android:text part. The whole line stays as the black text colour. Im sure im not missing anything as the string is all colour coded and so is the last string that I referred to while following the examples in the book which is:
android:text="#string/hello" />
And this is what is confusing me. So please point out the obvious or not so obvious thing that I have done wrong. Any help at all will be appreciated
Android uses the XML format to define interfaces and such. The line
android:text="#string/AppName" />
is not valid XML, and the black syntax is Eclipse's way of showing this. This is probably because you have forgotten to put some lines above it. What you want to have is something like this:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/AppName" />
This tells Android that you would like to put a TextView (or, a text label or whatever) with your string in it. Also note that the android:layout_width and android:layout_height attributes here are always required: without them, you will get an error and you can't build your application.
If you are not already familiar XML, I would highly recommend taking a look at an XML tutorial (for instance at Tizag or W3Schools) and learn the basics of XML, since understanding the XML language simplifies Android programming a lot.
If you want your text to have color, you can set android:textColor="######" in your xml either textview or whatever you used to display the string. ###### is your color code.
I want to create a hyperlink to textview in android. So in my layout I have given android:clickable="true" to the textview. Previously it worked for my other layouts. But now it is not working. When I am clicking on the textview it remained as it is.What may be the problem?
Please help me.
Have you tried Linkify class? Here is explanation from android developers. Also android:autoLink property of TextView might be helpful/
You can use textview.setOnClickListener. This is useful for you may be.
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.