Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Emailing Text from a View

Author: Wagied Davids
Published? true -- FormatLanguage: W

Problem:

You want to send an Email containing text or images from a View. The data to be emailed is passed as a parameter using an Intent.

Solution:

  1. Modify AndroidManifest.xml to allow for internet connection allowing email to be sent.
  2. Create the the visual presentation layer with Email Button which the user clicks.
  3. Attach an OnClickListener to allow the email to be sent when the user clicks the Email button.

Discussion:

File: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.examples"
	android:versionCode="1"
	android:versionName="1.0">

	<!-- Required Permission  -->
	<uses-permission
		android:name="android.permission.INTERNET" />

	<application
		android:icon="@drawable/icon"
		android:label="@string/app_name">
		<activity
			android:name=".Main"
			android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>

	</application>
</manifest> 

File: main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<Button
		android:id="@+id/emailButton"
		android:text="Email Text!"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
	</Button>
	
	<TextView
		android:id="@+id/text_to_email"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="@string/my_text" />

</LinearLayout>

File: strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string
		name="hello">Hello World, Main!</string>
	<string
		name="app_name">EmailAndroid</string>
	<string
		name="my_text">
		"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
		Ipsum has been the industry's standard dummy text ever since the 1500s, when
		an unknown printer took a galley of type and scrambled it to make a type
		specimen book. It has survived not only five centuries, but also the leap into
		electronic typesetting, remaining essentially unchanged. It was popularised in
		the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
		and more recently with desktop publishing software like Aldus PageMaker
		including versions of Lorem Ipsum." -- http://www.lipsum.com/
</string>
</resources>

File: Main.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity implements OnClickListener
	{
		private static final String tag = "Main";
		private Button emailButton;

		/** Called when the activity is first created. */
		@Override
		public void onCreate(Bundle savedInstanceState)
			{
				super.onCreate(savedInstanceState);

				// Set the View Layer
				setContentView(R.layout.main);

				// Get referenc to Email Button
				this.emailButton = (Button) this.findViewById(R.id.emailButton);

				// Sets the Event Listener onClick
				this.emailButton.setOnClickListener(this);

			}

		@Override
		public void onClick(View view)
			{
				if (view == this.emailButton)
					{
						Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
						emailIntent.setType("text/html");
						emailIntent.putExtra(android.content.Intent.EXTRA_TITLE, "My Title");
						emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");

						// Obtain reference to String and pass it to Intent
						emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, getString(R.string.my_text));
						startActivity(emailIntent);
					}
			}
	}

Download:

The source code for this project is in the Android Cookbook repository at http://github.com/IanDarwin/Android-Cookbook-Examples, in the subdirectory EmailTextView.
FlinxSYS 2011-12-08 05:01:10.134 link provided says file no longer available... ?