Cdacians

Cdacians
Cdacians

Friday, 14 September 2012

Simple eMail Activity



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. :)
New Visitor? Like what you read? Then do subscribe to our blog
Subscribe
Let us begin with the development..
Layout creation:
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?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:
1
2
3
4
5
6
7
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:
1
2
3
4
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:
1
2
3
4
5
buttonSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                        }
        });
Include the validation and email send logic inside button listener class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//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:
1
2
3
4
//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();
}
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.
Final output should look like this:

Thanks
akm
www.cdacians.com

No comments:

Post a Comment