46

I have a TextView with android:autoLink="email".

If I put my email address in there then a link appears that I can click.

How do I have different text appear (for example 'Send Feedback') instead of the email address but still behave the same when clicked?

Thanks

neildeadman
  • 3,974
  • 13
  • 42
  • 55

5 Answers5

67

To achieve what I wanted required a different approach:

TextView feedback = (TextView) findViewById(R.id.TextViewSendFeedback);
feedback.setText(Html.fromHtml("<a href=\"mailto:ask@me.it\">Send Feedback</a>"));
feedback.setMovementMethod(LinkMovementMethod.getInstance());

This basically places HTML in the TextView so I get a link saying 'Send Feedback' but clicking it opens the default email application.

Word of warning: Trying this in the emulator didn't initially work for me, saying it was unsupported. This was just because I didn't have an email account setup. Setting one up in the emulator made the link work as I wanted.

neildeadman
  • 3,974
  • 13
  • 42
  • 55
  • 1
    One thing to watch out for, is that this won't work if you set the android:autoLink to "all". It probably doesn't work if you set android:autoLink to anything except "none", but I haven't tested that. – Frank Harper Jul 28 '12 at 08:28
  • 2
    If you store the string in a string resource you need to replace the left angle bracket with < – ErikAndren Jul 19 '13 at 15:27
  • If there is no any mail app it will crash your app: https://stackoverflow.com/questions/54982981/mailto-crashes-an-app-if-user-doesnt-have-mail-app – user25 Mar 04 '19 at 12:14
  • I believe my warning at the bottom of my own answer states this already, but thanks. – neildeadman Mar 04 '19 at 12:34
52

You can use both links and email if you set the following param in the TextView

android:autoLink="web|email"

the links will be opened in the browser and the mails will be sent by the default mail client

Hakem Zaied
  • 14,061
  • 1
  • 23
  • 25
  • Does it handle exceptions? https://stackoverflow.com/questions/54982981/mailto-crashes-an-app-if-user-doesnt-have-mail-app – user25 Mar 04 '19 at 12:25
36

Another simple way in layout:

...
<TextView
        android:id="@+id/tvTelefone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sobre_telefone"
        android:textColor="#000000"
        android:autoLink="phone" />
...    

...
<string name="sobre_telefone">Contato: (45) 9145-0000</string>
} 

Read more here: http://developer.android.com/reference/android/widget/TextView.html#attr_android:autoLink

Dante
  • 742
  • 6
  • 10
  • 4
    This is in fact the best solution if you just need basic support for website and email address linking. For those confused with the answer, just check the provided `autoLink` property link. – cprcrack Dec 30 '13 at 13:05
  • 1
    To change the color of the link `textColorLink` needs to be used. – YBrush Mar 30 '18 at 18:03
1

It might be easier to create a button and inside your onClickListener() pull an email from maybe R.string.email.

bgs
  • 1,210
  • 10
  • 19
  • That is a very good suggestion that I never considered! I may actually use that, even though I have found the solution. Thanks! – neildeadman Jan 14 '11 at 08:54
-1

Fro the Strings From strings.xml :

<string name="your_string"><![CDATA[ contact us at <a href=\"mailto:recipient@mail.com\">recipient@mail.com</a> for more help.]]></string>


tvObject.setText(Html.fromHtml(getString(R.string.your_string)));
tvObject.setMovementMethod(LinkMovementMethod.getInstance());
Gopal
  • 1,734
  • 1
  • 14
  • 34