This example shows how to create an activity that displays a form you can fill out and use to send an email.
The fields expand according to the amount of data you enter. Their size is initialized by hints.
The data you use in an app can be provided from a database instead of EditText fields.
You can test the code on a device or set up email account/s in your AVD by doing the following:
Apps -> Menu -> System Settings -> Add Account -> Email -> Account Setup
Manifest permissions are not required because the code sends the email using an existing email client app already installed on your device.
Main Steps
1. Create an activity with three EditText widgets for the email address, subject, and message.
2. Add a button for sending the email.
2. Register the widgets with your XML layout file.
3. Create an onClickListener for the Send button.
4. In the public void onClick(View v) method, get the text from the three editTexts.
5. Create a new ACTION_SEND intent.
6. Use the intent's putExtra method to send the details from your EditTexts.
7. Set the intent's type to launch only email clients:
intent.setType("message/rfc822");
8. Use the intent createChooser method to display an application selection list, in this case available email clients.
9. When you tap the email client, your selected email client appears with the details you entered in the EditText widgets.
10. In your email client, tap Send.
For more information see:
http://developer.android.com/training/basics/intents/sending.html
Example Code
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button btn_send;
EditText et_emailAddress;
EditText et_subject;
EditText et_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setTitle("Send Email");
actionBar.setDisplayShowHomeEnabled(false);
btn_send = (Button) findViewById(R.id.btn_send);
et_emailAddress = (EditText) findViewById(R.id.et_emailAddress);
et_subject = (EditText) findViewById(R.id.et_subject);
et_message = (EditText) findViewById(R.id.et_message);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = et_emailAddress.getText().toString();
String subject = et_subject.getText().toString();
String message = et_message.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");
Intent chooser = Intent.createChooser(intent,
"Choose an Email client:");
startActivity(chooser);
}
});
}
}
Example XML Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:text="@string/email_address"
android:textColor="@android:color/holo_orange_light" />
<EditText
android:id="@+id/et_emailAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/email_address"
android:inputType="textEmailAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:text="@string/subject"
android:textColor="@android:color/holo_orange_light" />
<EditText
android:id="@+id/et_subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/subject" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:text="@string/message"
android:textColor="@android:color/holo_orange_light" />
<EditText
android:id="@+id/et_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/email_message" />
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send" />
</LinearLayout>