Kalid
Kalid

Reputation: 23

Problems when sending email with information taken from user input (android app)

I am building a small Android app that lets people donate food through an email client. After the user provides basic information (such as Name, Location, Food Type etc.), I can't seem to retrieve the input information to incorporate it inside the email text content. The space provided for the variables (of user input from EditText) keeps outputting "null" instead of displaying the user's information.

Here is XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<TextView
    android:id="@+id/textView7"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Name of Company / Individual:" />

<EditText
    android:id="@+id/etCompName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:textAllCaps="true"
    android:hint="Ex: Chicken Hut, Kaldi's Coffee, etc" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Telephone Number (required)" />

<EditText
    android:id="@+id/etPhone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Ex: 0922092161"
    android:inputType="phone" />

<TextView
    android:id="@+id/textView9"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Describe your Location:" />

<EditText
    android:id="@+id/etLocation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:textAllCaps="true"
    android:hint="Briefly describe where you are located..." />

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Food Type:" />

<EditText
    android:id="@+id/etFoodType"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:textAllCaps="true"
    android:hint="Ex: Local Food, Sandwiches, etc." />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Estimated Quantity (in grams):" />

<EditText
    android:id="@+id/etQuantity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:textAllCaps="true"
    android:hint="Ex: 1kg, 50g, etc." />

<TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Snap a Picture of Food (if available)" />

<ImageView
    android:id="@+id/ivFood"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    android:layout_weight="0.22" />

<Button
    android:id="@+id/bCam"
    style="@style/Widget.AppCompat.Button.Borderless.Colored"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="0dp"
    android:background="#ee9d3e"
    android:text="Open Camera"
    android:textColor="#ffffff" />

<Button
    android:id="@+id/bSubmit"
    android:background="#cecbc8"
    android:textColor="#ffffff"
    android:onClick="onButtonClickSend"
    style="@android:style/Widget.DeviceDefault.Button.Borderless"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Submit" />

     </LinearLayout>    

Here is Java:

   package com.example.android.foodshare;

   import android.content.Intent;

   import android.os.Bundle;
   import android.support.v7.app.AppCompatActivity;
   import android.view.View;
   import android.widget.EditText;


public class Donation extends AppCompatActivity {

EditText company_name;
EditText phone_number;
EditText location;
EditText foodType;
EditText quantity;

String companyNameString;
String phoneNumberString;
String locationString;
String foodTypeString;
String quantityString;

String emailTo = "[email protected]";
String emailSubject = "New Food Pickup Request From " + companyNameString; 
// maybe add from which company


String emailContent = "You have received a Food Pickup Request. Please find 
further details below:\n\n" +
        "Name of Organization (or Individual): " + companyNameString + "\n" 
         +
        "Telephone Number: " + phoneNumberString + "\n" +
        "Location: " + locationString + "\n" +
        "Available Food Type: " + foodTypeString + "\n" +
        "Quantity Available: " + quantityString;

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

    company_name = (EditText) findViewById(R.id.etCompName);
    phone_number = (EditText) findViewById(R.id.etPhone);
    location = (EditText) findViewById(R.id.etLocation);
    foodType = (EditText) findViewById(R.id.etFoodType);
    quantity = (EditText) findViewById(R.id.etQuantity);

}

public void onButtonClickSend(View v) {
    companyNameString = company_name.getText().toString();
    phoneNumberString = phone_number.getText().toString();
    locationString = location.getText().toString();
    foodTypeString = foodType.getText().toString();
    quantityString = quantity.getText().toString();

    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailTo});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailContent);



    //need this to prompts email client only
    emailIntent.setType("message/rfc822");
    startActivity(Intent.createChooser(emailIntent, "Select an Email Client:"));

}


     }

This is the output with "null" as part of my error:

*You have received a Food Pickup Request. Please find further details below:

Name of Organization (or Individual): null

Telephone Number: null

Location: null

Available Food Type: null

Quantity Available: null*

Thanks!

Upvotes: 0

Views: 25

Answers (2)

Mohammed Bakr Sikal
Mohammed Bakr Sikal

Reputation: 357

You are initializing your email content at declaration with null values and you are not changing it afterwards. Here:

String emailContent = "You have received a Food Pickup Request. Please find 
further details below:\n\n" +
        "Name of Organization (or Individual): " + companyNameString + "\n" 
         +
        "Telephone Number: " + phoneNumberString + "\n" +
        "Location: " + locationString + "\n" +
        "Available Food Type: " + foodTypeString + "\n" +
        "Quantity Available: " + quantityString;

I suggest you declare it first, then you do the above inside your onButtonClicked listener:

public void onButtonClickSend(View v) {
    companyNameString = company_name.getText().toString();
    phoneNumberString = phone_number.getText().toString();
    locationString = location.getText().toString();
    foodTypeString = foodType.getText().toString();
    quantityString = quantity.getText().toString();
emailContent = "You have received a Food Pickup Request. Please find 
further details below:\n\n" +
        "Name of Organization (or Individual): " + companyNameString + "\n" 
         +
        "Telephone Number: " + phoneNumberString + "\n" +
        "Location: " + locationString + "\n" +
        "Available Food Type: " + foodTypeString + "\n" +
        "Quantity Available: " + quantityString;
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailTo});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailContent);



    //need this to prompts email client only
    emailIntent.setType("message/rfc822");
    startActivity(Intent.createChooser(emailIntent, "Select an Email Client:"));

}

Upvotes: 1

Manish Kumar Sharma
Manish Kumar Sharma

Reputation: 13442

You have built the emailContent already as a field initialization when you construct an instance of Activity. Instead, just declare it and later initialize on button click like this:

String emailContent;


public void onButtonClickSend(View v) {
    companyNameString = company_name.getText().toString();
    phoneNumberString = phone_number.getText().toString();
    locationString = location.getText().toString();
    foodTypeString = foodType.getText().toString();
    quantityString = quantity.getText().toString();

    emailContent = "You have received a Food Pickup Request. Please find 
    further details below: \n\ n " +
    "Name of Organization (or Individual): " + companyNameString + "\n" +
        "Telephone Number: " + phoneNumberString + "\n" +
        "Location: " + locationString + "\n" +
        "Available Food Type: " + foodTypeString + "\n" +
        "Quantity Available: " + quantityString;

    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {
        emailTo
    });
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailContent);



    //need this to prompts email client only
    emailIntent.setType("message/rfc822");
    startActivity(Intent.createChooser(emailIntent, "Select an Email Client:"));

}

Upvotes: 1

Related Questions