TextView to send email when clicked

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


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


To achieve what I wanted required a different approach:

TextView feedback = (TextView) findViewById(R.id.TextViewSendFeedback);
feedback.setText(Html.fromHtml("<a href=\"mailto:[email protected]\">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.


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

Tags:

Android