Add data from activity to email message body - android

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);

Related

How do i get information typed on editText?

So I have an editText in an app I created, the aim of this editText is to send a piece of text information. I want the user to be able to send text information for me to use, it's kinda like when you have a "FeedBack" option and the user has to fill it with the required information needed, I want to be able to get this information somehow. Pls help, I'm still new to the mobile app development thing. :)
Use getText() on your EditText. This will return an Editable which can be casted to String if you want:
String feedback = editTextFeedback.getText().toString();
If you want to receive the input by mail:
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();
}
What you can do is opening a "share" activity and send the text through any app you have in the phone (whatsapp, telegram, mail, etc).
public void share(message: String) {
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Sending message");
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
Intent.createChooser(shareIntent, "Share with");
} catch (Exception e) {
e.printStackTrace()
}
}
This will open a window like this:

How to Make a Button Submit Multiple Textviews to my Email?

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.

android simple form taking data from form

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.

Android: insert subject with autoLink="email"

I use a textview with android:autoLink="email" to send an email being redirected to the smartphone Email or Gmail application. It works but I can't insert a subject. Are there people coming to do ?
You could send an email this way :
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, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
}
catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
}
And call this in the onClick event of the TextView.
You could also put some style to show it as a clickable link (underlined, colored).
TO will then contain the value of the TextView

Recipients are not showing in email screen in Android

Hi I am using in built email sending functionality.But when I tried the code.
void sendEmailMessage(String emailId)
{
Log.i(TAG, "emailId = "+emailId);
Intent intentEmail = new Intent(Intent.ACTION_SEND);
intentEmail.setType("text/plain");
String[] recipients = new String[]{emailId};
intentEmail.putExtra(Intent.EXTRA_EMAIL,recipients);
intentEmail.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
intentEmail.putExtra(Intent.EXTRA_TEXT, "body of email");
try
{
startActivity(Intent.createChooser(intentEmail, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}
}
I don't see recipient email address in recipient EditText on Email screen.I am not getting what I am doing wrong please help.
I think in your void sendEmailMessage(String emailId) method's emailId argument didn't have the value. Just check where you've been called from. And, make sure you are passing email id or not? For example. In your class's somewhere just call that method like below with value.
sendEmailMessage("mail#mail.com");

Categories

Resources