Java – Populate emails with form entries Eclipse Android

Populate emails with form entries Eclipse Android… here is a solution to the problem.

Populate emails with form entries Eclipse Android

I’m new to coding, this is my first app and I want to fill the body of the email with entries from my form. I’ve been trying to get it to work all day, just haven’t figured it out yet, and any help would be appreciated. Log entries in the console reflect form input, but do not populate the email.
Thanks in advance,

Here is my current code.

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/scrollView1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<TextView
        android:id="@+id/appDiscription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/discription_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

<Spinner
        android:id="@+id/employeeSpinner"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:entries="@array/employeeSpinner"
        android:prompt="@string/employeeSpinner" />

<Spinner
        android:id="@+id/jobTypeSpinner"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:entries="@array/jobTypeSpinner"
        android:prompt="@string/jobTypeSpinner" />

<EditText
        android:id="@+id/jobDateText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/job_date_text"
        android:inputType="date" >

<requestFocus />
    </EditText>

<EditText
        android:id="@+id/clientText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/client_text"
        android:inputType="textPersonName" />

<EditText
        android:id="@+id/arrivalText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/arrival_text"
        android:inputType="time" />

<EditText
        android:id="@+id/departureText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/departure_text"
        android:inputType="time" />

<EditText
        android:id="@+id/assignmentText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/assignment_text" 
        android:inputType="textPersonName"/>

<EditText
        android:id="@+id/notesText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/notes_text"
        android:inputType="textMultiLine" />

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="onClick"
        android:text="@string/submit_button" />
 </LinearLayout>


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

public class AssignmentTracker extends Activity {

Spinner employeeSpinner;
Spinner jobTypeSpinner;
EditText clientText;
EditText jobDateText;
EditText arrivalText;
EditText departureText;
EditText assignmentText;
EditText notesText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

employeeSpinner = (Spinner) findViewById(R.id.employeeSpinner);
    jobTypeSpinner = (Spinner) findViewById(R.id.jobTypeSpinner);
    clientText = (EditText) findViewById(R.id.clientText);
    jobDateText = (EditText) findViewById(R.id.jobDateText);
    arrivalText = (EditText) findViewById(R.id.arrivalText);
    departureText = (EditText) findViewById(R.id.departureText);
    assignmentText = (EditText) findViewById(R.id.assignmentText);
    notesText = (EditText) findViewById(R.id.notesText);

}
public void onClick(View v) {
    String employeeType = employeeSpinner.getSelectedItem().toString();
    String jobType = jobTypeSpinner.getSelectedItem().toString();
    String nameText = clientText.getText().toString();
    String dateText = jobDateText.getText().toString();
    String arrivalType = arrivalText.getText().toString();
    String departureType = departureText.getText().toString();
    String assignText = assignmentText.getText().toString();
    String noteText = notesText.getText().toString();

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "Daily Worksheet");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Employee:"
            + employeeSpinner.getSelectedItem().toString());

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Job Type:"
            + jobTypeSpinner.getSelectedItem().toString());

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Date:"
            + jobDateText.getText().toString());

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Client:"
            + clientText.getText().toString());

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Arrival Time:"
            + arrivalText.getText().toString());

emailIntent.putExtra(android.content.Intent. EXTRA_TEXT,
            "Departure Time:" + departureText.getText().toString());

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Assignment:"
            + assignmentText.getText().toString());

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Notes:"
            + notesText.getText().toString());
    emailIntent.setType("plain/text");
    startActivity(emailIntent);

Log.d("AssignmentTracker", "onClicked " + employeeType);
    Log.d("AssignmentTracker", "onClicked " + jobType);
    Log.d("AssignmentTracker", "onClicked " + nameText);
    Log.d("AssignmentTracker", "onClicked " + dateText);
    Log.d("AssignmentTracker", "onClicked " + arrivalType);
    Log.d("AssignmentTracker", "onClicked " + departureType);
    Log.d("AssignmentTracker", "onClicked " + assignText);
    Log.d("AssignmentTracker", "onClicked " + noteText);

Solution

You can only set fields such as email address, subject, and body.

You can compose a body from a field you have and send it like this.

try {
        final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("plain/text");
        if (recipient != null)  intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{to});
        if (subject != null)    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        if (message != null)    intent.putExtra(android.content.Intent.EXTRA_TEXT, message);

startActivity(Intent.createChooser(intent, "Send mail..."));

} catch (ActivityNotFoundException e) {

}

Related Problems and Solutions