Pr.Pg Next Pg

Email Application tutorials

  • In this tutorial we are going to make an activity where we will insert the data for an Email like email id, subject, message, etc.

 

Making a new Layout

  • We first make a layout with text boxes for each field and then we also have a button.

  • We also introduce a Scroll View which will help the user scroll through the text boxes.

  • We also have a button which will on clicking will open the Email application on the android phone and copy the contents into the respective fields.

  • We also add a few extra redundant EditText Views so that the user can experience scrolling.

  • You can access the XML code from this link email.xml

  • The Scroll View is generally used with Linear Layout vertically.

  • The scroll View will be used to scroll throug the Text Boxes.

  • We also add a button to send the email via the default android mail application.

 

Creating the Activity

  • You can access the Java Code from this link Email.java.

  • We have basically set up the activity and here we have implemented the View.onClickListener class.

  • Since we have implemented the class, we have to define all its methods.

  • So we define the onClick() for the button Send

  • This is just another method to define the OnClick for a button. It will function in the same way as before.

  • We have assigned the references using the initalizeVars() method.

  • In the onClick() method we have stored the Strings entered by the user into appropriate variables declared at the start of the class.

  • We also make an array of a string and store the email address entered by the user in that array.

  • We then make a new Intent variable to start the default email activity on the phone.

  • We do this by sending android.content.Intent.ACTION_SEND as the parameter.

  • Activity Action: Deliver some data to someone else. Who the data is being delivered to is not specified; it is up to the receiver of this action to ask the user where the data should be sent.

  • We use the putExtra() method to pass the data to the another activity like email address, subject and the main Message.

  • The email addresses have to be sent in terms of a string array, thatís why we have made a String array named addresses, even though our tutorial example uses just one email address.

  • EXTRA_EMAIL: A String[] holding e-mail addresses that should be delivered to.

  • We also have to set the type of the data being sent. We do that by using the method

  • setType() :Set an explicit MIME data type.

  • Finally we add the activity to our List which we had created before and then start the activity from there.

 

Main.xml

 

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity">

 

<TextView

android:id="@+id/lblemailid"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:text="Email Id" />

 

<EditText

android:id="@+id/txtemail"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_marginLeft="27dp"

android:layout_toRightOf="@+id/lblemailid"

android:ems="10"

android:inputType="textEmailAddress">

 

<requestFocus />

</EditText>

 

<TextView

android:id="@+id/lblsubject" android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/lblemailid"

android:layout_below="@+id/txtemail"

android:layout_marginTop="27dp"

android:text="Subject" />

 

<EditText

android:id="@+id/txtsubject"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBaseline="@+id/lblsubject"

android:layout_alignBottom="@+id/lblsubject"

android:layout_alignLeft="@+id/txtemail"

android:ems="10" />

 

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/lblsubject"

android:layout_below="@+id/txtsubject"

android:layout_marginTop="30dp"

android:text="Body" />

 

<EditText

android:id="@+id/txtbody"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/textView1"

android:layout_alignRight="@+id/txtsubject"

android:layout_below="@+id/textView1"

android:layout_marginTop="25dp"

android:ems="10"

android:inputType="textMultiLine" />

 

<Button

android:id="@+id/btnsend"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/txtbody"

android:layout_centerHorizontal="true"

android:layout_marginTop="70dp"

android:text="Send" />

 

</RelativeLayout>

 

 

Activity.java

 

package com.example.email;

 

import android.net.Uri;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.content.Intent;

 

public class MainActivity extends Activity {

Button btnsend;

EditText edemail,edsubject,edbody;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnsend=(Button)findViewById(R.id.btnsend);

edemail=(EditText)findViewById(R.id.txtemail);

edsubject=(EditText)findViewById(R.id.txtsubject);

edbody=(EditText)findViewById(R.id.txtbody);

btnsend.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

String emailid=edemail.getText().toString();

String subject=edsubject.getText().toString();

String body=edbody.getText().toString();

Intent emailIntent = new Intent(Intent.ACTION_SEND);

emailIntent.setData(Uri.parse("mailto:"));

emailIntent.putExtra(Intent.EXTRA_EMAIL, emailid);

emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);

emailIntent.putExtra(Intent.EXTRA_TEXT, body);

emailIntent.setType("message/rfc822");

startActivity(Intent.createChooser(emailIntent, "Email"));

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

 

}

 

Note

  • This application can't be run on an emulator because there is no email application in a Virtual Device.

  • The Screenshots below illustrate the working of the application.

 

Pr.Pg border                                              Next Pg