Teams Winter 2011/team1/Android/Send Email

From CDOT Wiki
Jump to: navigation, search

9. Add Send Email functionality

9.1 Create the Email menu item

9.1.1 Add the String value for the layout title to String.xml:

<string name="emailContact_textView">Email Contact</string>

9.1.2 Now lets finish our layout email.xml.
EmailLayout.jpg

9.1.3 Here is the email.xml implementation:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textSize="30dp"
        android:layout_gravity="center"
        android:text="@string/emailContact_textView" />

    <TextView android:id="@+id/emailTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/emailTo_textView" />

    <EditText android:id="@+id/editEmailTo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView android:id="@+id/emailSubject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/emailSubject_textView" />

    <EditText android:id="@+id/editEmailSubject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
       
    <TextView android:id="@+id/emailBody"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/emailBody_textView" />
       
    <EditText android:id="@+id/editEmailBody"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button android:id="@+id/emailSend"
        android:layout_column="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnEmailSend" />
</LinearLayout>

9.1.4 Now Add the Email menu Item. Navigate to menu.xml and add the menu item.

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item1" android:title="Add Student"></item>
    <item android:id="@+id/item2" android:title="Delete Student"></item>
    <item android:id="@+id/item3" android:title="View Student"></item>
    <item android:id="@+id/item4" android:title="Edit Student"></item>
    <item android:id="@+id/item5" android:title="Send E-mail"></item>
</menu>

9.1.5 Now it is time to build your project so that our id's will be generated.

9.2 Creating EmailContactActivity.java

9.2.1 Lets add our private variables and that we get the right layout once it has been started.

private EditText sEmailTo;
    private EditText sSubject;
    private EditText sBody;
    private Button btnSend;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.email);
        setContentView(R.layout.email);

9.2.2 If we capture the intent now we will be able to retrieve the saved email address of the selected student.

// get the intent sent to this class
        Intent i = this.getIntent();
       
        sEmailTo = (EditText) findViewById(R.id.editEmailTo);
        //sEmailTo.setText(i.getStringExtra("oldEmail"));
       
        sSubject = (EditText) findViewById(R.id.editEmailSubject);
        sBody = (EditText) findViewById(R.id.editEmailBody);

9.2.3 In order to have our application send the email we must implement a button.

btnSend.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (v.getId() == R.id.emailSend) {
                    finish();
                }
            }
        });
       
    }

9.2.4 We must now override the finish() method and send the email.

@Override
    public void finish() {
        // Setup the recipient in a String array
        String[] mailto = { sEmailTo.getText().toString() };
        // Create a new Intent to send messages
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        // Add attributes to the intent
        sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
        sendIntent.putExtra(Intent.EXTRA_SUBJECT,
                sSubject.getText().toString());
        sendIntent.putExtra(Intent.EXTRA_TEXT,
                sBody.getText().toString());
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, "SendMail"));

        super.finish();
    }

9.2.5 Some screens
LayoutStrings.jpg ListOptions.jpg