portfoliobuilder portfoliobuilder - 5 days ago 5
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

Answer

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 ";
message=message+getResources().getText(R.string.myemail);
final SpannableString mes = new SpannableString(message);
Linkify.addLinks(mes, Linkify.EMAIL_ADDRESSES);

//TextView       
final TextView tx1=new TextView(this);
tx1.setText(mes);
tx1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
tx1.setAutoLinkMask(RESULT_OK);
tx1.setMovementMethod(LinkMovementMethod.getInstance());

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

Comments