portfoliobuilder portfoliobuilder - 7 months ago 65
Android Question

Hyperlink email address in strings.xml file?

How do I hyperlink email addresses so that when clicked it takes user to a composed email from my strings.xml. In my strings.xml file I am hyperlinking text for normal urls with the following:

<![CDATA[<a href="http://google.com">Google website example</a>]]>

I tried to use a similar technique for emails, however nothing is happening.

<![CDATA[<a href="mailto:example@gmail.com">Email example</a>]]>

What am I doing wrong? Thanks in advance


You could use the Linkify class. You will have regular strings in your xml file, eg:

<string name="myemail">something@gmail.com</string>

and then in your code:

String message="Some message ";
final SpannableString mes = new SpannableString(message);
Linkify.addLinks(mes, Linkify.EMAIL_ADDRESSES);

final TextView tx1=new TextView(this);
tx1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

Then, in the textview you will have a clickable link on the email text.