Sending contact form in android via email

  • Replies:0
comunidad mexicana
  • Forum posts: 3

Feb 1, 2014, 1:45:39 PM via Website

Hi all, hope in your help.

I am trying to make a basic contact form app where people can fill in the needed information and press the send button and have the information sent to a specific E-mail. I have put it together but for some reason it is not working and I don't have error in debug.

Here is the code please let me know what I can do to make it work.
Thank you

activity_contactos.xml
1<RelativeLayout xmlns:android="XXX"
2 xmlns:tools="XXX"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:paddingBottom="@dimen/activity_vertical_margin"
6 android:paddingLeft="@dimen/activity_horizontal_margin"
7 android:paddingRight="@dimen/activity_horizontal_margin"
8 android:paddingTop="@dimen/activity_vertical_margin"
9 tools:context=".MainSendEmail" >
10
11 <TextView
12 android:id="@+id/textView1"
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"
15 android:layout_marginTop="20dp"
16 android:gravity="center"
17 android:text="@string/form"
18 android:textAppearance="?android:attr/textAppearanceMedium" />
19
20 <EditText
21 android:id="@+id/etName"
22 android:layout_width="match_parent"
23 android:layout_height="wrap_content"
24 android:layout_alignLeft="@+id/textView1"
25 android:layout_below="@+id/textView1"
26 android:layout_marginTop="20dp"
27 android:ems="10"
28 android:hint="@string/name"
29 android:inputType="textPersonName" >
30
31 <requestFocus />
32 </EditText>
33
34 <EditText
35 android:id="@+id/etPhone"
36 android:layout_width="match_parent"
37 android:layout_height="wrap_content"
38 android:layout_below="@+id/etName"
39 android:layout_marginTop="20dp"
40 android:ems="10"
41 android:hint="@string/phone"
42 android:inputType="phone" />
43
44 <EditText
45 android:id="@+id/etEmail"
46 android:layout_width="match_parent"
47 android:layout_height="wrap_content"
48 android:layout_below="@+id/etPhone"
49 android:layout_marginTop="20dp"
50 android:ems="10"
51 android:hint="@string/email"
52 android:inputType="textEmailAddress" />
53
54 <EditText
55 android:id="@+id/etAdd"
56 android:layout_width="match_parent"
57 android:layout_height="wrap_content"
58 android:layout_below="@+id/etEmail"
59 android:layout_marginTop="20dp"
60 android:ems="10"
61 android:hint="@string/additional_information"
62 android:inputType="textMultiLine"
63 android:lines="6" />
64
65 <Button
66 android:id="@+id/send"
67 android:layout_width="match_parent"
68 android:layout_height="wrap_content"
69 android:layout_alignParentBottom="true"
70 android:text="@string/send"
71 android:width="150sp" />
72
73 </RelativeLayout>

MainSendEmail.java
1package com.ccmex.comunidadmexicana;
2
3import android.app.Activity;
4import android.content.Intent;
5import android.os.Bundle;
6import android.text.Html;
7import android.util.Log;
8import android.view.Menu;
9import android.view.View;
10import android.widget.Button;
11import android.widget.EditText;
12import android.widget.Toast;
13
14public class MainSendEmail extends Activity {
15 EditText etname, etphone, etemail, etadd;
16
17 @Override
18 protected void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.activity_contactos);
21
22 etname = (EditText) findViewById (R.id.etName);
23 etphone = (EditText) findViewById(R.id.etPhone);
24 etemail = (EditText) findViewById(R.id.etEmail);
25 etadd = (EditText)findViewById(R.id.etAdd);
26
27 Button startBtn = (Button) findViewById(R.id.send);
28 startBtn.setOnClickListener(new View.OnClickListener() {
29 public void onClick(View view) {
30
31 if (etname.getText().toString().length() == 0) {
32 etname.setError( "Vul uw naam in" );
33 }
34 else
35 {
36 sendEmail();
37 }
38 }
39 });
40
41 }
42
43 protected void sendEmail() {
44 Log.i("Send email", "");
45
46 String[] TO = {"XXX@gmail.com"};
47 String[] CC = {"XXX@gmail.com"};
48
49 String body=
50 "Name : "+etname.getText().toString()+"<br>Mobile :"+etphone.getText().toString()+
51 "<br>Email :"+etemail.getText().toString()+"<br>Bericht :"+etadd.getText().toString();
52
53 Intent email = new Intent(Intent.ACTION_SEND);
54 email.putExtra(Intent.EXTRA_EMAIL, TO);
55 email.putExtra(Intent.EXTRA_CC, CC);
56 email.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
57 email.setType("message/rfc822");
58 startActivityForResult(Intent.createChooser(email, "marketing"),1);
59
60 try {
61 startActivity(Intent.createChooser(email, "Send mail..."));
62 finish();
63 Log.i("Finished sending email...", "");
64 } catch (android.content.ActivityNotFoundException ex) {
65 Toast.makeText(MainSendEmail.this,
66 "There is no email client installed.", Toast.LENGTH_SHORT).show();
67 }
68 }
69
70 @Override
71 public boolean onCreateOptionsMenu(Menu menu) {
72 getMenuInflater().inflate(R.menu.main, menu);
73 return true;
74 }
75 }

— modified on Feb 1, 2014, 1:54:50 PM

Reply