I have an app and want to have a contact us for where the user inputs their name, address, phone number, and a comment section. Then they will click on compose mail button and it will load the text into the email automaticlly. have some of the code worked out but not sure how to get the text from the edit text into my email msg. anyone have any suggestions on how i can do this. I have attached the code from my xml file and my java file.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Contactform extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactform);
Button email = (Button) findViewById(R.id.button30);
email.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
//TODO Auto-generated method stub
Intent email = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
email.setType("plain/text");
email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ladsoffice@lads-to-leaders.org"});
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Lads to Leaders/Leaderettes Questions and/or Comments");
email.putExtra(android.content.Intent.EXTRA_TEXT, "Sent from the Lads to Leaders/Leaderettes Android App");
/* Send it off to the Activity-Chooser */
startActivity(Intent.createChooser(email, "Send mail..."));
}
});
}
}
and here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#bf311a" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="@string/name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="37dp"
android:layout_toRightOf="@+id/textView1"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="35dp"
android:text="@string/addy"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentRight="true"
android:inputType="textPostalAddress" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText2"
android:layout_marginTop="30dp"
android:textColor="#000000"
android:text="@string/cell"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText2"
android:inputType="phone" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:textColor="#000000"
android:text="@string/questions"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView4"
android:layout_marginTop="25dp"
android:inputType="textMultiLine" />
<Button
android:id="@+id/button30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText4"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:background="@drawable/composemail" />
</RelativeLayout>
and here is a ss of what my form looks like if that helps
Thank you for any help you can give. Still learning a bit on how to do some of this stuff so i apologize if this has been asked before but been searching for two days and can't seem to find what i need to do this.
Ok for those who want to know here is the corrected code. The only code i needed to modify was the code for the Java file so thats the only code i will post.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Contactform extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactform);
final EditText name = (EditText) findViewById(R.id.editText1);
final EditText addy = (EditText) findViewById(R.id.editText2);
final EditText cell = (EditText) findViewById(R.id.editText3);
final EditText questions = (EditText) findViewById(R.id.editText4);
Button email = (Button) findViewById(R.id.button30);
email.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
//TODO Auto-generated method stub
Intent email = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
email.setType("plain/text");
email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ladsoffice@lads-to-leaders.org"});
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Lads to Leaders/Leaderettes Questions and/or Comments");
email.putExtra(android.content.Intent.EXTRA_TEXT,
"name:"+name.getText().toString()+'\n'+"address:"+addy.getText().toString()+'\n'+"phone:"+cell.getText().toString()+'\n'+'\n'+questions.getText().toString()+'\n'+'\n'+"Sent from the Lads to Leaders/Leaderettes Android App.");
/* Send it off to the Activity-Chooser */
startActivity(Intent.createChooser(email, "Send mail..."));
}
});
}
}
Ok so just a quick explaintion. you have to declare your edit text in the code after your oncreate method. It must say final in front of it as well. Then you input your strings into the intent for EXTRA TEXT. also i added the '\n' in single quotes. this action will act as an enter button giving each item its own line so for example i only wanted name on one line so i did name+mystring+'\n'. this will make your name and string appear on one line and then go down one line for the next line. Hope this makes since and it helps someone else out.
You can put all data inside email.putExtra(android.content.Intent.EXTRA_TEXT, "Sent from the Lads to Leaders/Leaderettes Android App");
First Get all EditText by ID here in oncreate Like
EditText name = (EditText) findViewById(R.id.editext1);
EditText address = (EditText) findViewById(R.id.editext2);
EditText phone = (EditText) findViewById(R.id.editext3);
Then put all data
email.putExtra(android.content.Intent.EXTRA_TEXT,
"name:"+name.getText().toString()+"address:"+address.getText().toString()+"phone:
"+phone.getText().toString());
Enjoy :)
来源:https://stackoverflow.com/questions/9882680/adding-text-from-edit-text-field-into-an-email