Views or components for this user interface are three EditText. One EditText allows the user to input the recipient e-mail address; One is to input the subject and another one is for inputting the message. The content of the main_layout.xml that is the resource of the user interface is shown below:
main_layout.xml file
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
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" >
<EditText
android:id="@+id/mail_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/mail_address" />
<EditText
android:id="@+id/mail_subject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/mail_subject" />
<EditText
android:id="@+id/mail_text"
android:layout_width="fill_parent"
android:layout_height="200sp"
android:hint="@string/mail_text"
android:gravity="top"
/>
</LinearLayout>
The send and attach icons are placed on the action bar (see the picture above). By clicking the send icon on the action bar, a list of mail clients installed on your system is displayed to receive your message to send it to the recipient. The attach icon allows the user to attach a file to be sent along the e-mail message.
To setup action bar for the e-mail sender app, the SherlockActionBar library is used. If you don't know how to add this project library to your project, please read this TextViewer post.
When the user selects the attach icon from the action bar, a file chooser dialog shows up so that the user can choose any file that he/she wants to attach to the e-mail message. The FileChooser class that created in the previous post (FileChooser) is integrated with the e-mail sender app to show the file chooser dialog. The steps below tell you how to integrate the FileChooser with this app.
1. Copy the selection_style.xml, fileicon.png, and diricon.png files to the res/drawable directory
2. Copy the listlayout.xml file to the res/layout directory
3. Copy the AndFileChooser.java and ListAdapterModel.java files to the src directory and rename the package to match the package name of the e-mail sender app.
4. In the res/menu directory, replace the auto-generated main.xml file with the main.xml file from the FileChooser app.
The AndroidManifest.xml file is modified to apply icon (mmail) and use the Shecklock sheme in the e-mail sender app. The content of this file is written as shown below.
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emailsender"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/mmail"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light.DarkActionBar">
<activity android:configChanges="orientation"
android:name="com.example.emailsender.MainActivity"
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>
Now, we take a look at the MainActivity.java file. In this file, code are written to display file chooser dialog and receive the selected file path, and to send the e-mail message.
MainActivity.java file
package com.example.emailsender;
import java.util.List;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import android.net.Uri;
import android.os.Bundle;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.widget.EditText;
public class MainActivity extends SherlockActivity {
AndFileChooser filechooser;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
protected void onStart(){
super.onStart();
filechooser=new AndFileChooser("/",this); //create file chooser dialog
}
public void sendmail(){
//get the selected file path
String att_path=filechooser.getFilePath();
//get address, subject, and message input by the user
EditText txto=(EditText) findViewById(R.id.mail_address);
EditText txtsubject=(EditText) findViewById(R.id.mail_subject);
EditText txtmessage=(EditText) findViewById(R.id.mail_text);
//create a Uri object to for the selected file path
Uri urlpath=Uri.parse("file://"+att_path);
//create an intent object for sending action
Intent intent=new Intent(Intent.ACTION_SEND);
//specify the minetype of the e-mail message
intent.setType("*/*");
//put the recipient e-mail address in the intent object
intent.putExtra(Intent.EXTRA_EMAIL,txto.getText().toString());
//put the subject in the intent object
intent.putExtra(Intent.EXTRA_SUBJECT,txtsubject.getText().toString());
//put the message in the intent object
intent.putExtra(Intent.EXTRA_TEXT,txtmessage.getText().toString());
//put attached file in the intent object
intent.putExtra(Intent.EXTRA_STREAM,urlpath);
//Find available apps to receive the intent and start the intent if find one
PackageManager pm=getPackageManager();
List<ResolveInfo> vapps=pm.queryIntentActivities(intent, 0);
if(vapps.size()>0){
startActivity(Intent.createChooser(intent,"Send mail"));
}
}
public void browse(){
filechooser.show(); //show the file chooser dialog
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.send: //send item is selected
sendmail();
break;
case R.id.attach: //attach item is selected
browse();
break;
}
return true;
}
}
In the onStart method, a AndFileChooser object is created by the following line of code.
filechooser=new AndFileChooser("/",this); //create file chooser dialog
To handle which item of the action bar is selected, you need to override the onOptionsItemSelected method. The switch... case statement is used to determine the selected item. The browse method is called to show the file chooser dialog when the selected item is the attach item. Otherwise, the sendmail method is called to send the e-mail message to the e-mail client.
In th sendmail method, the getPath method of the AndFileChooser object is used to get the path of the selected file. The e-mail data such as recipient e-mail address, subject, message, and attached file are collected and placed in the intent object to be sent to the mail client. You need to specify ACTION_SEND to the intent for e-mail sending. The PackageManager class to read the mail clients installed on the system and show a list of the available mail client apps. The startActivity method is called to start sending the e-mail message.
Now you are ready to run the e-mail sender app on your emulator. The apk file of this app is also provided through the link below so you can download and install it on your real device.
Download the apk file of the e-mail sender app