Can't get HREF to work in Android strings.xml - android

I'm trying to add a link to a Twitter profile in an about box. 'Regular' links such as email address and web address are handled by
android:autoLink="email|web"
in about.xml, but for a Twitter profile page I need to use html code in my strings.xml. I've tried:
<string name="twitter">Follow us on <a href=\"http://www.twitter.com/mytwitterprofile">Twitter: #mytwitterprofile</a></string>
which renders html markup on the about box.
I've also tried:
<string name="twitter">Follow us on Twitter: #mytwitterprofile</string>
which display the text "Follow us on Twitter: #mytwitterprofile", but it is not a hyper-link.
How do I do this seemingly simple task!?
Cheers,
Barry

The problem is your "a href" link tags are within strings.xml and being parsed as tags when strings.xml is parsed, which you don't want. Meaning you need to have it ignore the tags using XML's CDATA:
<string name="sampleText">Sample text <![CDATA[link1]]></string>
And then you can continue with Html.fromHtml() and make it clickable with LinkMovementMethod:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());

The simple answer is that the TextView does not support <a> tags. AFAIK, it only supports basic formatting such as <b>, <i> and <u>. However, if you supply android:autoLink="web", the following string:
<string name="twitter">Follow us at twitter.com/mytwitterprofile</string>
Will turn twitter.com/mytwitterprofile into a proper link (when set via XML like android:text="#string/twitter"; if you want to set it from code, you'll need the Html.fromHtml method someone else posted in an answer).

I'm not too sure how to link using 'strings', but you could set text of the EditText or TextView using fromHtml...
TextView text = (TextView) findViewById(R.id.text);
text.setText(Html.fromHtml("Google Link!"));
text.setMovementMethod(LinkMovementMethod.getInstance());

Related

Unable to get links identified in a message string along with link preview in TextView

I am working on a chat application. In this when a user copies text from somewhere and tries to paste in the EditText and send it using the send Button. The links in the text are not identified. When I set the textview to clickable and android:autoLink="web".
Still it's not working and I am also not getting a preview of the link like this one below:
.
And help, would be great like how to proceed.Thank you !
Here you go for html link:
TextView textView = findViewById(R.id.text_view);
Spanned spanned = Html.fromHtml("your html string");
textView.setText(spanned, TextView.BufferType.SPANNABLE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Make sure your don't have android:autoLink="web" https://stackoverflow.com/a/15299208/1329854
And for rich link preview check this answer:
https://stackoverflow.com/a/38217502/1329854

Sending a mail when clicking on a link

This is the html element in my Strings.xml file.
<string name="five">
<![CDATA[
<HTML>
<Body>
<p>.................Some Text.......................
<br><br>Feel free to contact us at<br> <font color="#add8e6">myemailid#gmail.com</font></p>
</body>
</HTML>
]]>
</string>
Now, when the user clicks on myemailid#gmail.com, I want the yahoo mail or gmail or which ever mail application is present in the user's phone to open up and the myemailid#gmail.com to fill up the TO : field. Is anything like this possible?
When you are setting the text you can use (assuming you have retrieved the string into a variable called text and are referenciing a TextView called textView)
textView.setText(Html.fromHtml(text));
When setting your layout you can also declare android:autolink="email" on the Views holding the five string.
The description of autolink says "Controls whether links such as urls and email addresses are automatically found and converted to clickable links." i.e. the text within the View is parsed and any (in this case) email addresses in the text are converted to clickable links.
In the case of android:autolink="email" your other Html tags in the text wouldn't be processed and would be display as they are above (rather than as a processed HTML document).
Whereas Html.fromHtml() will handle the HTML tags and deal with correctly formated tags.
Have a look at Androids Linkify class.
As an example, if you do something like:
Linkify.addLinks(myTextView, Linkify.EMAIL_ADDRESSES);
any email address in the textview will be clickable and when clicked, an appropriate dialog will be shown to the user.
Ofcourse, this is assuming that your string is actually displayed inside a TextView

Which HTML tags are supported by Android TextView?

Android's TextView class can display formatted text via HTML.fromHtml() as explained for example here: HTML tags in string for TextView
The TextView class can only deal with a small subset of HTML, but I do not know which tags and attributes are supported and which are not. The summary given here: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html does not seem to be correct. E.g. <div align="..."> does NOT work for me using Android 2.2
Looked it up for everyone searching for it.
Date: July 2017
Source: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/Html.java
Html.fromHtml supports:
p
ul
li
div
span
strong
b
em
cite
dfn
i
big
small
font
blockquote
tt
a
u
del
s
strike
sup
sub
h1
h2
h3
h4
h5
h6
img
br
I noticed that this article:
https://web.archive.org/web/20171118200650/http://daniel-codes.blogspot.com/2011/04/html-in-textviews.html
lists <div> as being supported by Html.fromHtml(), but it doesn't show support for the "align" attribute.
(Other supported attributes are shown for tags on that page.)
The author says he constructed the reference by looking at code in the git repositories for Android.
Edit:
Over time, it appears the list of supported tags has changed. See this later post for example: http://www.grokkingandroid.com/android-quick-tip-formatting-text-with-html-fromhtml/ .
Based on both those articles, I'd suggest that examining the source code seems to be the most reliable way to get the recent information.
The best approach to use CData sections for the string in strings.xml file to get a actual display of the html content to the TextView the below code snippet will give you the fair idea.
//in string.xml file
<string name="welcome_text"><![CDATA[<b>Welcome,</b> to the forthetyroprogrammers blog Logged in as:]]> %1$s.</string>
Java code
String welcomStr=String.format(getString(R.string.welcome_text),username);
tvWelcomeUser.setText(Html.fromHtml(welcomStr));
CData section in string text keeps the html tag data intact even after formatting text using String.format method. So, Html.fromHtml(str) works fine and you’ll see the bold text in Welcome message.
Output:
Welcome, to your favorite music app store. Logged in as: username
Since it is constantly being updated, the best way to track which HTML tags are supported in Android is to check the source code of Html.java

Android-Is it possible to add a clickable link into a string resource

I usually set up some kind of AlertDialog to fire off when a user first uses one of my apps and I explain how to use the app and give an overall introduction to what they just downloaded. I also usually load my strings from a strings.xml file.
What I want to do is make one of the words in my string resource clickable like a hyperlink on a web page. Basically you'd have an AlertDialog and within the string resource there would be a highlighted word or possibly just a web address that they could press. I suppose I could just add a button that would take them to the site but I just wanted to know if making a word in your string resource a clickable hyperlink was possible.
Just use an HTML format link in your resource:
<string name="my_link">Click me!</string>
You can then use setMovementMethod(LinkMovementMethod.getInstance()) on your TextView to make the link clickable.
There is also TextView's android:autoLink attribute which should also work.
I found something interesting. Let me know if any of you observed this.
Below hyperlink is not working if you use
android:autoLink="web"
but works with
TextView link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());
<string name="my_link">
<a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource">
Click me!
</a>
</string>
but if you use the following link it works with both
android:autoLink="web" (or)
TextView link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());
<string name="my_link">
<a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource">
http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource
</a>
</string>
As answered by #Nikolay Elenkov In Kotlin I used it from string resources in this way (detailed way for freshers):
in my layout place a checkbox:
<CheckBox
android:id="#+id/termsCB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="#dimen/spacing_normal"
android:layout_marginTop="#dimen/spacing_normal"
android:text="#string/terms_and_conditions" />
in strings.xml
<string name="terms_and_conditions">I read and accept the Terms and Conditions</string>
in my activity class inside the onCreate() method:
termsCB.movementMethod = LinkMovementMethod.getInstance()
Android doesn't make strings that contain valid link clickable automatically. What you can do, is add custom view to your dialog and use WebView to show the alert message. In that case, you can store html in your resources and they will be clickable.
View alertDialogView = LayoutInflater.inflate(R.layout.alert_dialog_layout, null);
WebView myWebView = (WebView) alertDialogView.findViewById(R.id.dialogWebView);
myWebView.loadData("Google!", "text/html", "utf-8");
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setView(alertDialogView);
alert_dialog_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<WebView android:id="#+id/dialogWebView" android:layout_height="wrap_content"
android:layout_width="wrap_content" />
For me, the hyperlink always works well, when I:
Add these two lines to the activity/fragment:
textView.setMovementMethod(LinkMovementMethod.getInstance())
textView.setText(Html.fromHtml(getString(R.string.link)))
Do not add anything to the xml (like autoLink etc).
Define link in strings.xml like
<string name="link"><![CDATA[Google link]]></string>

Android TextView enable LinkiFy and Href tags

I want to set a text to the textview which contains href tags as well as normal http links. For ex, text like this "please <a href=/'http://google.com' target='_blank'>click here</a>. or visit http://yahoo.com".
The issue is I am unable to set both properties together. If I set Html.fromHtml to the text, the link with the href tag is highlighted. But the Linkfy property is not working for "http://yahoo.com" and vice versa. Is there any default property to enable both href tags and normal links in a TextView.
Thanks,
I couldn't find one myself as I worked through a similar situation with HREFs and telephone numbers; in lieu of anything better, I am currently using a custom REGEX for calling Linkfiy.addLinks. Calling Linkify.addLinks with a mask "also removes any existing URLSpans attached to the Spannable", which were created by Html.fromHtml. See the Linkify Documentation.
If it's possible using an mx controls Label (spark does not support this any longer) u can use htmlText like the following example:
import mx.controls.Label //class need to be imported
var textview:Label = new Label();
textview.htmlText = "please <a href=/'http://google.com' target='_blank'>click here</a>. or visit http://yahoo.com";
yourItemContainer.addElement(textview);
This should work. But I don't know if it's possible for u using a normal label.

Categories

Resources