Sending a mail when clicking on a link - android

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

Related

How to unhyperlink links programmatically?

I have a large body of text that includes web urls and emails. I created a SpannableString object and used Linkify class to set up hyperlinks for the web urls and emails. However, perceived phone numbers also get linked. So even though I have instances of 5-digit numbers such as "12345", this numeric sequence also are being linked. When clicked, a prompt comes up asking to call the number. Linkify thnks it is a phone number. How can I go back through and un-link specific text that I know should not be linked?
Here is how I have linked everything
final SpannableString s = new SpannableString(context.getText(R.string.message));
Linkify.addLinks(s, Linkify.WEB_URLS);
textView.setText(s);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Thank you in advance.

Bold a text and have it in next line for a particular string in Strings.xml

I am trying to bold a particular word in a string that is stored in strings.xml:
Here is what I would like to have it as:
Future tense: Example: I will be there at 9:00AM
I followed this: Bold words in a string of strings.xml in Android - But didn't get the results.
so I had text in strings.xml like:
<string name="futureText">Future tense: <b>Example:</b> I will be there at 9:00AM</string>
Spanned text = Html.fromHtml(res.getString(R.string.futureText));
questionText.setText (text);
I am not getting any bold for "Example:".
Also how to get the "Example: I will be there at 9:00AM" text to next line? I know in string \n goes to next how about in html format?
What is wrong here?
You can not really read the html tag within the string.xml because it is a default tag within the xml so by the time you read it programatically it wont read the tag but just its text without the tag.
solution:
you need to wrap it in <![CDATA[ ...raw html... ]]> to enable you to read the html tag within the text.
sample:
<string name="futureText">Future tense: <![CDATA[<b>Example:</b>]]> I will be there at 9:00AM</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.

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

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());

where i put html content in android to view

I have parse the url and i get the html contents i want to show that content as a web page then what can i do to show that html contents. I am using textView then it shows same html content with tags.
I have following html content which i want to display in webView. It displays only
**// Please** but its original output is not this.
<br /><br />Read the handouts please for tomorrow.<br /><br /><!--
homework help
homework help
help with homework
homework assignments
elementary school
high school
middle school
// --><font color="#60c000" size="4"><strong>Please!</strong></font>
its original output is
Read the handouts please for tomorrow.
Please!
I have other html also where there is a tag of img but this shows nothing
Please Suggest me how to solve this problem
If you want to use the html coding and view just the content without the html tag and that too in textview you have to use this syntax in the java file :
TextView textView = (TextViewPlus) findViewById(R.id.textViewId);
textView.setText(Html.fromHtml(getString(R.string.htmlString)));
the above line will fetch the string content in the string.xml file and in string.xml file you can give the content in html format , something like this :
<string name="htmlString"><![CDATA[
<div>
<p>hello this can be you any content and even you can use the other html tags.</p>
</div>
]]></string>
let me remind you the content has to be inclosed between: <[CDATA[......your html code goes here........]]>
There are two ways. Either go for a webview which can encode HTML tags automatically or if you want to use Textview you can use Html.fromHtml() method available on Android....Then set the Text to your Textview. Something like this
Textview.setText(Html.fromHtml(source));
or Even like this
Spanned t=Html.fromHtml("HTML String that needs to be displayed");
yourtextView.setText(t);

Categories

Resources