portfoliobuilder
portfoliobuilder

Reputation: 7856

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:[email protected]">Email example</a>]]>

What am I doing wrong? Thanks in advance

Upvotes: 1

Views: 3030

Answers (2)

Alyas Hajri
Alyas Hajri

Reputation: 35

you could use other way, you can use basic HTML in string as shown in example

<string name="send_mail"><![CDATA[ <a href="mailto:[email protected]">email us</a>]]></string>

Then create AlertDialog as shown in example

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Data")
    .setIcon(R.drawable.info)
   .setMessage(Html.fromHtml(R.string.send_mail)
   .setCancelable(true)
   .setNegativeButton("OK",null);

AlertDialog welcomeAlert = builder.create();
welcomeAlert.show();
// Make the textview clickable. Must be called after show()
((TextView)welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

for more details , please look to this Question Link

Upvotes: 0

jvlach
jvlach

Reputation: 565

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

<string name="myemail">[email protected]</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.

Upvotes: 1

Related Questions