-1

There's a section within the app I'm creating that allows users to input information into eight different TextFields. Those fields are:

  1. Name
  2. Address
  3. City
  4. State
  5. Zip
  6. Phone
  7. Email
  8. Age

Now when this information is completed and the user taps the submit button I'm either wanting the information to be sent to my personal email address directly after hitting the button or storing the information to be viewed for a later time. I'm relatively new to Android development, however, I was thinking that I should do the following within a method that I would call when the button is tap like so:

final EditText moverName = (EditText)findViewById(R.id.nameRegistration);
String name = regName.getText().toString();

I've searched and searched and can't find anything that I'm trying to accomplish. Is there possibly a way to send the information directly to my email? or do I need to store it somehow for later viewing. Heres my Java file I have so far:

public class UserRegistrationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_registration);
    }

    public void sendFeedback(View button) {

        final EditText userName = (EditText)findViewById(R.id.nameRegistration);
        String name = regName.getText().toString();

        final EditText userStreetAddress = (EditText)findViewById(R.id.userStreetAddressRegistration);
        String address = userStreetAddress.getText().toString();

        final EditText userCity = (EditText)findViewById(R.id.userCityRegistration);
        String city = userCity.getText().toString();

        final EditText userState = (EditText)findViewById(R.id.userStateRegistration);
        String state = userState.getText().toString();

        final EditText userZip = (EditText)findViewById(R.id.userZipRegistration);
        String zip = userZip.getText().toString();

        final EditText userPhone = (EditText)findViewById(R.id.userPhoneRegistration);
        String phone = userPhone.getText().toString();

        final EditText userEmail = (EditText)findViewById(R.id.userEmailRegistration);
        String email = userEmail.getText().toString();

        final EditText userDOB = (EditText)findViewById(R.id.userDOB);
        String dob = userDOB.getText().toString();

    }
}

Thanks in advance!

shmosel
  • 49,289
  • 6
  • 73
  • 138
mur7ay
  • 803
  • 3
  • 16
  • 44

2 Answers2

2

In your case you have 2 options to send email:

  1. Send email using the app: In this case only thing you can do is launch email app with all the information you want to send pre-populated in the email body. But, in this case you have to press final "Send" again. You can see the details here - How to open Email program via Intents (but only an Email program)

  2. Integrate with any email providers like MailChimp, sendgrid etc and make an API call directly from your app or through your backend.

Community
  • 1
  • 1
Puran
  • 984
  • 6
  • 15
1

I have replicated your problem with this solution, It is not tested but I believe it will point you to a good direction.

To save within your app, you might do this

Layout file

<?xml version="1.0" encoding="utf-8"?>
<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="com.inducesmile.emailapplication.MainActivity">

<EditText
   android:id="@+id/nameRegistration"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="Enter name"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"/>

<EditText
    android:id="@+id/userStreetAddressRegistration"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Address"
    android:layout_below="@+id/nameRegistration"
    android:layout_marginTop="8dp"
    android:layout_centerHorizontal="true"/>
<EditText
    android:id="@+id/userCityRegistration"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter City"
    android:layout_below="@+id/userStreetAddressRegistration"
    android:layout_marginTop="8dp"
    android:layout_centerHorizontal="true"/>

<EditText
    android:id="@+id/userStateRegistration"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter State"
    android:layout_below="@+id/userCityRegistration"
    android:layout_marginTop="8dp"
    android:layout_centerHorizontal="true"/>


<EditText
    android:id="@+id/userZipRegistration"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Zip Code"
    android:layout_below="@+id/userStateRegistration"
    android:layout_marginTop="8dp"
    android:layout_centerHorizontal="true"/>

<EditText
    android:id="@+id/userPhoneRegistration"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Phone Number"
    android:layout_below="@+id/userZipRegistration"
    android:layout_marginTop="8dp"
    android:layout_centerHorizontal="true"/>

<EditText
    android:id="@+id/userEmailRegistration"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Email"
    android:layout_below="@+id/userPhoneRegistration"
    android:layout_marginTop="8dp"
    android:layout_centerHorizontal="true"/>

<EditText
    android:id="@+id/userDOB"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter DOB"
    android:layout_below="@+id/userEmailRegistration"
    android:layout_marginTop="8dp"
    android:layout_centerHorizontal="true"/>

<Button
    android:id="@+id/submit_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:padding="20dp"
    android:layout_marginTop="16dp"
    android:layout_below="@+id/userDOB"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

Activity Page

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

private EditText userName;
private EditText userStreetAddress;
private EditText userCity;
private EditText userState;
private EditText userZip;
private EditText userPhone;
private EditText userEmail;
private EditText userDOB;

private SharedPreferences prefs;

private boolean hasDataBeenSaved;

private Set<String> set;

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

    prefs=this.getSharedPreferences("settings", Context.MODE_PRIVATE);
    set = prefs.getStringSet("Personal Information", null);
    if(set.size() > 0){
        hasDataBeenSaved = true;
    }

    if(hasDataBeenSaved){
        // display the store personal information

    }

    userName = (EditText)findViewById(R.id.nameRegistration);
    userStreetAddress = (EditText)findViewById(R.id.userStreetAddressRegistration);
    userCity = (EditText)findViewById(R.id.userCityRegistration);
    userState = (EditText)findViewById(R.id.userStateRegistration);
    userZip = (EditText)findViewById(R.id.userZipRegistration);
    userPhone = (EditText)findViewById(R.id.userPhoneRegistration);
    userEmail = (EditText)findViewById(R.id.userEmailRegistration);
    userDOB = (EditText)findViewById(R.id.userDOB);


    Button submitButton = (Button)findViewById(R.id.submit_button);
    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(!hasDataBeenSaved){
                // Data has not been save then save them

                String name = userName.getText().toString();
                String address = userStreetAddress.getText().toString();
                String city = userCity.getText().toString();
                String state = userState.getText().toString();
                String zip = userZip.getText().toString();
                String phone = userPhone.getText().toString();
                String email = userEmail.getText().toString();
                String dob = userDOB.getText().toString();

                if(!isEmpty(name) || !isEmpty(address) || !isEmpty(city) || !isEmpty(state) || !isEmpty(zip) || !isEmpty(phone) || !isEmpty(email) || !isEmpty(dob)){
                    Toast.makeText(MainActivity.this, "All input field must be filled", Toast.LENGTH_LONG).show();
                    return;
                }

                Set<String> set = new HashSet<String>();
                set.add(name);
                set.add(address);
                set.add(city);
                set.add(state);
                set.add(zip);
                set.add(phone);
                set.add(email);
                set.add(dob);

                SharedPreferences.Editor edit = prefs.edit();
                edit.putStringSet("Personal Information", set);
                edit.commit();
            }
        }
    });
}

private boolean isEmpty(String input){
    if(input.equals("")){
        return true;
    }
    return false;
}
}

For sending the information to your email, you can either use the

  1. Android Intent

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    

In this case, it will just launch a client email application

  1. If you want to use a button click to send email with all user inputted information available, then refer to this link - Sending Email in Android using JavaMail API without using the default/built-in app
Community
  • 1
  • 1
Inducesmile
  • 2,475
  • 1
  • 17
  • 19