Simple eMail Activity

In this post, we are going to develop a simple email application where you can send email.

What it is gonna do?

You have to enter the email ID, subject, message and hit send, on hitting send button you will be prompted with list of email clients installed in your mobile to choose. Choose email client,say GMail, information you typed in our application will be taken to GMail application. Now all you have to do is hit ‘Send’ in GMail application. 🙂

Let us begin with the development..

Quick Links

Code Listings

Layout creation:
[pgsubscribe]
Application will have:

Three edittext controls – 1. email ID  2. subject  3. message

One button – Send

I have created layout for this application and the layout.xml of the application  is given below:

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

    <TextView
        android:id="@+id/textViewPhoneNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextTo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textViewSubject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subject"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextSubject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>

    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />
</LinearLayout>

 Layout view:

We are done with UI, let us begin code the logic.

Create button and edittext objects to refer the resources from layout.xml:

	Button buttonSend;
        //Recipient Email ID 
	EditText txtTo;
        //Subject
	EditText txtSubject;
        //Message body
	EditText txtMessage;

Refer the controls we created in layout.xml to handle the events:

buttonSend = (Button) findViewById(R.id.buttonSend);
		txtTo = (EditText) findViewById(R.id.editTextTo);
		txtSubject = (EditText) findViewById(R.id.editTextSubject);
		txtMessage = (EditText) findViewById(R.id.editTextMessage);

Create listener class for the ‘Send’ button to handle button click event inside onCreate method:

buttonSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
                        }
		});

Include the validation and email send logic inside button listener class:

 
        //String object to store Recipient email ID               
        String to = txtTo.getText().toString();
                //String object to store Subject
		String subject = txtSubject.getText().toString();
                //String object to store Message
		String message = txtMessage.getText().toString();
                //If email ID is not entered, display toast message
		if (to != null && to.length() == 0) {
			Toast.makeText(getApplicationContext(),
					"You forgot to enter the email ID",
					Toast.LENGTH_SHORT).show();
                //If entered email ID is not valid, display toast message
		} else if (to != null && to.length() > 0 && !isEmailValid(to)) {
			Toast.makeText(getApplicationContext(),
					"Entered email ID is not Valid", Toast.LENGTH_SHORT)
							.show();
                //If subject is empty, display toast message
		} else if (subject != null && subject.length() == 0) {
			Toast.makeText(getApplicationContext(),
					"You forgot to enter the subject",
					Toast.LENGTH_SHORT).show();
                //If message is empty, display toast message
		} else if (message != null && message.length() == 0) {
			Toast.makeText(getApplicationContext(),
					"You forgot to enter the message",
					Toast.LENGTH_SHORT).show();
                //If all three controls are entered with valid values
		} else if (to != null && subject != null && message != null) {
			Intent email = new Intent(Intent.ACTION_SEND);
			email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });				
			email.putExtra(Intent.EXTRA_SUBJECT, subject);
			email.putExtra(Intent.EXTRA_TEXT, message);				
			email.setType("message/rfc822");
			startActivity(Intent.createChooser(email,
						"Choose an Email client :"));

		}

Boolean method to validate the email ID:

//Boolean method to check if entered email ID is valid or not
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

[pglinkadssmall1]

It’s time to run our application, connect the mobile device to system through USB. Right click on project >> Run as >> Android application.

Android application(.apk) will be installed in the device and start the activity.

Demo

Final output should look like this:



Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.

 

[pglinkadssmall]

Share your comments below!!..

Also do let me know your suggestions and feedback through mail


[pgfeedback]
[pgwriteforus]

Related Articles

Author: Udhay

Share This Post On
468 ad
  • farhan shah

    Nice and awsome tutorial..Thanks you

Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares