So I've created a form and I want to send that data from form using a button. In activity_main.xml I created the design, some textboxs and a button. Now where I write the code to send the data to a gmail? Like, I'm new to android but how I take the data from every textbox?
Inside your button onClick() method you should get text from TextView or EditText that you want data from like this
String name=textviewName.getText().toString();
try this String name = editTextName.getText().toString(); for every editText and you can do whatever you want with String object.
Try to read a little bit from the Android docs.
edittext.getText();
Android docs on EditText
To Mail Something Which is present on your Text Field , you can use below content ..
use this code to that Activity where the values present and send button..
sendEmail is Email Send Btn ID
protected void sendEmail() {
Log.i("Send email", "");
String[] TO = {""};
String[] CC = {""};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, " Your Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, " Your Text Here"+((EditText) findViewById(R.id.editTextID)).
getText().toString());
emailIntent.setType("message/rfc822");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
}
catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(CurrentActivityName.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
}}
you can use multiple TextView Values or EditText Values in EXTRA_TEXT by Joining + sign.
Related
I looked it up, and this is the most common way to send an email...
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients
installed.",
Toast.LENGTH_SHORT).show();
}
I am confused as to what this is actually sending, and how to make this happen when the user clicks a button. Do I put the recipient email (me) like this?
i.putExtra(Intent.EXTRA_EMAIL , "myemail#gmail.com");
Then the same format for subject and message/body of the email?
Is this how I would put the user's input into the body of the email? (The user inputs multiple editText boxes)
editText userTitle = (editText)findViewById(R.id.idOfTheEditTextBox);
editText userDescription = (editText)findViewById(R.id.idOfTheEditTextBox);
Then input it like this?
i.putExtra(Intent.EXTRA_TEXT , "userTitle", "userDescription");
Finally, what does all that toast and no email clients installed mean? I am new to android app development, and am making an app on Android Studio! All help is very much appreciated!! Thanks!
Firstly "Toast"
Toast code is written to give alert to user, notifying that no email
client is available in users phone (ex. gmail, etc)
Now the main portion,
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
Yes this above line add the recipents email address.
So to achive this kind of thing, you need to do following things,
Create layout with one Edittext (where user can put recepeints email address which you can pass to Intent). And one button on click of which you will launch Intent.
Now then the button is clicked write this code:-
public class MainActivity extends AppCompatActivity {
EditText etRecipentId, etSubject, etBody;
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etRecipentId = (EditText) findViewById(R.id.email_id);
etSubject = (EditText) findViewById(R.id.et_subject);
etBody = (EditText) findViewById(R.id.et_body);
}
b1.setOnClickListener(new OnClickListener() {
public void onClick() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[] {
etRecipentId.getText().toString();
});
i.putExtra(Intent.EXTRA_SUBJECT, etSubject.getText().toString(););
i.putExtra(Intent.EXTRA_TEXT, etBody.getText().toString(););
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients
installed.",
Toast.LENGTH_SHORT).show();
}
}
});
instead of i.putExtra(Intent.EXTRA_TEXT , "userTitle", "userDescription"); use i.putExtra(Intent.EXTRA_TEXT , userTitle+" "+userDescription);
And as you are getting exception ,check that is there any email client like gmail,etc is installed in your phone or not.
In Android Studio I wish to send an email on a button click. I am using the following code till I work out what is going on before I start changing thing.
String[] TO = {"ABC#yahoo.com.au"};
String[] CC = {"xyz#gmail.com"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Some message added in here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
This works fine and well and shows up on my phone with the email content being as expected, however the email content, "Some message added in here" line is clearly hardcoded. I obviously wish to add in my own content by doing the following
String content = "Information I want to send";
emailIntent.putExtra(Intent.EXTRA_TEXT, content);
But for some reason the email content is blank. Why does is a string "content" recognised but a String variable x is not?
Check This Examples
By Looking at your code i only found problem in setting
emailIntent.setType(text/plain).
May Be you are using Gmail for sending mails(So you have to check second example).
Send Email (to Phone Email Client)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some#email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
Send Email (to Gmail)
Gmail does not examine the extra Intent fields, so in order to use this intent, you need to use the Intent.ACTION_SENDTO and pass a mailto: URI with the subject and body URL encoded.
String uriText =
"mailto:youremail#gmail.com" +
"?subject=" + Uri.encode("some subject text here") +
"&body=" + Uri.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send email"));
I feel dumb for having to ask this but I cannot find how to take UI information from multiple EditText views and place in an email body. FYI I have the intent down, I just want to populate the message body.
Intent buildingfireemail = new Intent(android.content.Intent.ACTION_SEND);
buildingfireemail.setType("plain/text");///.setType("message/rfc822")
buildingfireemail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"to#email.com"});
buildingfireemail.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
buildingfireemail.putExtra(android.content.Intent.EXTRA_TEXT, "Text"
//////////////I need to add data from 80+ views into here.
);try {
startActivity(Intent.createChooser(buildingfireemail, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(BuildingFireActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
Try this :
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , editText.getText().toString());
try
{
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients installed.",Toast.LENGTH_SHORT).show();
}
Create a function which returns the whole String from all your text edits. For example:
private String getText() {
String text = "";
text += mEditText1.getText().toString() + "\n";
text += mEditText2.getText().toString() + "\n";
return text;
}
Use like:
buildingfireemail.putExtra(android.content.Intent.EXTRA_TEXT, getText());
Init member variables for class:
private EditText mEditText1;
Take all edit texts to member variables in onCreate after setContentView:
mEditText1 = (EditText) findViewById(R.layout.editText1);
I want to select a number of email addresses and then send an email to all of them.
My code is as below:
emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{listofemailaddresses});
emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
emailIntent .putExtra(android.content.Intent.EXTRA_TEXT, Constants.SMS_MESSAGE);
this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));`
listofemailaddresses is a string which contains all the emails separated by a ',' sign. But the To field is always empty in this.
Add this line to your code:
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "appsupport#YOUR_DOMAIN.com" });
This will fill the "To" section of your screen.
If you having the list of email addresses seprated by , then split that string to get individual email id as follow:
String [] emailList = emailAddresses.split(",");
now use emailList with your Intent.EXTRA_EMAIL key,as this will show all email addresses inside to field of send email form.
How about this code:
final Intent emailLauncher = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailLauncher.setType("message/rfc822");
emailLauncher.putExtra(Intent.EXTRA_EMAIL, emailList);
emailLauncher.putExtra(Intent.EXTRA_SUBJECT, "check this subject line");
emailLauncher.putExtra(Intent.EXTRA_TEXT, "hey check this message body!");
try{
startActivity(emailLauncher);
}catch(ActivityNotFoundException e){
}
Intent intent = null;
intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL,new String[] { "abc#gmail.com" , "test#gmail.com", "xyz#test.com"});
startActivity(intent);
I have been working with Java and Xml for a few months now, and have learned a great deal thanks to everyones help on StackOverflow.
My question is about java programming for android in relation to the submit button.
Currently I am trying to figure out how to submit a value to an email address (behind the scenes)
Lets say we have a text field and a button; I want to take the value entered in the text field, and submit that to an email address onclick.
I am unable to find anything online that shows me how to do this.
Thank you in advance for reading through my post and I look forward to your suggestions.
This is a great example of how using Intents can come in great handy!
Android has a bunch of pre-defined Intents that do certain things within the system; you may have clicked on a picture before and a dialog popped up asking whether you would like to view it in your gallery or in a third-party app such as Astro. The viewing of an image has its own pre-determined intent.
Sending an email also has its own pre-determined intent: android.content.Intent.ACTION_SEND. You'll need to create an intent with that property and then attach extra information (ie. the address to send to, the subject/message body, etc.).
Example code:
// Data members
private Intent emailIntent;
private String feedback;
private EditText feedbackBox;
// Create the Intent, and give it the pre-defined value
// that the Android machine automatically associates with
// sending an email.
emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
// Put extra information into the Intent, including the email address
// that you wish to send to, and any subject (optional, of course).
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"your_email#whatever.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Insert subject here");
// Acquire feedback from an EditText and save it to a String.
feedback = feedbackBox.getText().toString();
// Put the message into the Intent as more extra information,
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, feedback);
// Start the Intent, which will launch the user's email
// app (make sure you save any necessary information in YOUR app
// in your onPause() method, as launching the email Intent will
// pause your app). This will create what I discussed above - a
// popup box that the user can use to determine which app they would like
// to use in order to send the email.
startActivity(Intent.createChooser(emailIntent, "Insert title for dialog box."));
I hoped this helped!!
Some sources you might like to check out:
http://developer.android.com/guide/topics/intents/intents-filters.html
http://developer.android.com/reference/android/content/Intent.html#ACTION_SEND
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, someEditText.getText());
startActivity(Intent.createChooser(emailIntent, "Send someone an email..."));
try this
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:Type email address here"));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
It works good
String mail=mailid.getText().toString();
String msubject=subject.getText().toString();
String mbody=body.getText().toString();
Log.i("Send email", "");
String[] TO = {mail};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Static subject "+ msubject);
emailIntent.putExtra(Intent.EXTRA_TEXT, "Static body "+ mbody);
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
}