Contact Form in an App Using ACTION_SEND Intent

Send an Email from an Android App Using Intents

This article covers the sending of an email from an Android App, useful for support and feedback purposes. Developing successful Apps requires more than good ideas and good code. You also need to support your users and respond to their questions, suggestions and bug reports. One way is to provide a contact, feedback or suggestion form in your App. Usually accessible from a menu option or button. In this example the first activity is the contact form.

One of the good things about programming with Android is the ability to reuse existing functionality with a few lines of code. The use of Intents allows the programmer to access other Apps code without directly linking to them or common libraries. It is achieved via a type of internal messaging system and similar to declarative programming. This is the case with sending an email from an App. All that is needed is for the App to set up an Intent with the required recipient, subject and text and hand it over to Android to let the email client do the rest.

Example Contact Form Project

To follow this tutorial start by creating a new Android Project in Eclipse using the File menu, select New then Android Project. Fill in the Project Name, here Contact Form is used. Click Next and select the build target from the installed APIs, e.g. Android 1.5, any of the installed APIs will work and it can be changed later if required. Click Next and enter the Package Name in the required format, e.g. biz.tekeye.contactform, leave Create Activity checked with the default Activity name, click Finish.

Some EditTexts will hold the email address, subject title and content. TextViews are used to provide labels. A button hands the email over to the devices mail client. Here is an example of a suitable layout that can be used to replace the code in main.xml in the res\layout folder (if copying and pasting this code see Copying Code from the Articles for tips):

[code lang=”xml”]<!–?xml version="1.0" encoding="utf-8"?–>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To:-"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText android:id="@+id/etTo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject:-"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText android:id="@+id/etSubject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message:-"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText android:id="@+id/etMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="6"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button android:id="@+id/btnOK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>[/code]

In the code (in the class under the src folder) we grab a reference to the EditTexts, add a listener for the button. In the listener an Intent is created with ACTION_SEND. The Intent is loaded with the address, subject and message text. The type is set to message/rfc822 so only email clients will be interested in handling the Intent. Here’s the code:

[code lang=”Java”]public class ContactFormActivity extends Activity {
Button btnOK;
EditText txtTo;
EditText txtSubject;
EditText txtMessage;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnOK = (Button) findViewById(R.id.btnOK);
txtTo = (EditText) findViewById(R.id.etTo);
txtTo.requestFocus();
txtSubject = (EditText) findViewById(R.id.etSubject);
txtMessage = (EditText) findViewById(R.id.etMessage);
btnOK.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String to = txtTo.getText().toString();
String subject = txtSubject.getText().toString();
String message = txtMessage.getText().toString();
Intent mail = new Intent(Intent.ACTION_SEND);
mail.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
mail.putExtra(Intent.EXTRA_SUBJECT, subject);
mail.putExtra(Intent.EXTRA_TEXT, message);
mail.setType("message/rfc822");
startActivity(Intent.createChooser(mail, "Send email via:"));
}
});
}
}[/code]

Imports need to be added for Button, EditText, OnClickListener, View and Intent.

Send Email From Android App

When the button is pressed and more than one application is on the device that can handle sending email you’ll be offered a choice:

The static function createChooser is used so that all email Apps are listed each time. If just startActivity(mail); was used an option to always use a specific email App is presented.


The email can then be tweaked in the actual email App before finally sending it on its way:

If you run an App with such a contact form on the emulator (the AVD, Android Virtual Device) you will not be able to send the email. You will need to test on a physical device:

Improving the Contact Form

It is unlikely the email address needs to change. A default can be used which saves on one EditText, just let the user know they will be sending via Email. Likewise the subject could be preset with the name of the App. For small screen support it may be wise to replace the labels with hints. These changes would result in a simpler screen:

If required the email Intent also supports CC (Carbon Copy) and BCC (Blind Carbon Copy) using EXTRA_CC and EXTRA_BCC:

[code lang=”Java”]mail.putExtra(Intent.EXTRA_CC, new String[]{"someoneelse@example.com"});
mail.putExtra(Intent.EXTRA_BCC, new String[]{"someonesecret@example.com"});[/code]

The user could be given fields to enter the CC and BCC address or they could be set to default values.

See also the article About Box for an Android App to see how Linkify can be used to make an email address clickable.

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.