0

I have two textboxes: one for the subject and the other for the actual message. Unfortunately, the code I'm using does not pass any information when I test it on my phone running android 4.2.2 with the latest gmail app.

Updated method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.tipus);
findViewById(R.id.sendemailbutton).setOnClickListener(emailclick);
findViewById(R.id.cancelbtn).setOnClickListener(myclick);
subjectline = (EditText) findViewById(R.layout.tipus);
emailcontent = (EditText) findViewById(R.layout.tipus);

}

public final Button.OnClickListener emailclick = new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);  

        String aEmailList[] = { oliuremail, vikemail };   
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);  //supposed to pass info of the emails to the email app
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subjectline.getText().toString());  //not taking stuff from an edittext, only things in quotes. 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailcontent.getText().toString());  //won't accept anything from an edittext.
        emailIntent.setType("plain/text");
        startActivity(Intent.createChooser(emailIntent, "Send Email Using...")); 
    }

};

tipus.xml (Updated)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/messagetitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/emailmessage"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="46dp"
    android:text="Message:" />

<EditText
    android:id="@+id/emailmessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="122dp"
    android:ems="10"
    android:hint="Write your message here."
    android:inputType="textMultiLine" />

<Button
    android:id="@+id/cancelbtn"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/arrowrightblack" />

<Button
    android:id="@+id/sendemailbutton"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Send" />

<EditText
    android:id="@+id/subjectline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/subjectdenoter"
    android:layout_alignBottom="@+id/subjectdenoter"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:hint="Please Add a Subject." />

<TextView
    android:id="@+id/subjecttitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignRight="@+id/textView3"
    android:layout_marginBottom="82dp"
    android:text="Subject:" />

When I use this code, the info gets passed to the email app. However, I have two textboxes in which the user will type in a subject and then type in the actual text of the email. I need the info passed from the two text boxes to be sent to the email app. I've looked over stackoverflow and tutsplus and they had some info but the ones that talked about passing data from textboxes were not very informative. Thanks in advance.

EDIT:

I finally found out the answer. You must make a private string such as:

private String getSubjectContent() {
    String text = "";

    text += yourtexteditname.getText().toString() + "\n";

    return text;
}

NOTE: You can make as many of these methods in the class. In each method, just replace "yourtexteditname" with the name of the textEdit. You can also put multiple textEdits into one method if you want to get info from multiple textEdits and put them into one place in the email (Subject line, message body etc.)

Then, all you have to do is add the normal code to start an intent. Like this:

public final Button.OnClickListener emailclick = new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        variablename = (EditText) findViewById(R.id.textviewname); //defining the textedit you want to get info from. 
        variablename = (EditText) findViewById(R.id.othertextviewname); //defining the textEdit you want to get info from.
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("message/rfc822"); //specifies message for email app.
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, getEmailContent()); //adds the actual content of the email by calling the method previously defined
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getSubjectContent()); //adds the subject by calling the method previously defined.
        startActivity(Intent.createChooser(emailIntent, "Title of the dialog chooser"));


    }

};
ThatGuyThere
  • 365
  • 2
  • 10
  • 23
  • Have ya tried [EditText.getText](https://developer.android.com/reference/android/widget/EditText.html#getText())? – adneal Mar 23 '13 at 05:02
  • what you have to do ? you required to get value from user used in the email info, as a extra_email and extra_text. – Mr.Sandy Mar 23 '13 at 05:04
  • Can you show where are your edittexts and how do you retrieve the data from those edittexts to put to intents? – Kanth Mar 23 '13 at 05:25
  • @adneal yes I have tried that, it does not work and crashes my application. – ThatGuyThere Mar 23 '13 at 05:29
  • @ThatGuyThere could you please update this question with 'tipus.xml' content as it is without modifying any thing. – vinay kumar Mar 23 '13 at 05:43
  • @vinaykumar Just updated it. – ThatGuyThere Mar 23 '13 at 06:43
  • @ThatGuyThere are you facing any exceptions here? First check whether you are able to get the text entered by the user in your activity by calling getText() on your respective edit-text since it return editable convert it to string (.toString()). – vinay kumar Mar 23 '13 at 07:14

2 Answers2

1

you can use this by using String varible. like..

String subjectdata = edittext.getText().toString();
String sharedata = edittext1.getText().toString();

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_SUBJECT, subjectdata);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@gmail.com"});
email.putExtra(Intent.EXTRA_TEXT, shareddata);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Send Mail :"));
Ajay
  • 1,189
  • 1
  • 12
  • 28
0

Because you are sending to multiple email recipients, I believe that you need to use Intent.ACTION_SEND_MULTIPLE instead of Intent.ACTION_SEND:

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

Some implementations also prefer Intent.ACTION_SENDTO.

(Optional) You may want to use this:

emailIntent.setType("message/rfc822");

Instead of this:

emailIntent.setType("plain/text");

Finally, retrieve the user's text entry from the EditText fields like this:

String <body-or-subject-text> = editText.getText().toString();
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
  • I'm sorry to be asking so many questions, but I am relatively new to android development and was wondering what I'm supposed to substitute in for ""? Also, I have seen that the order the intents are listed matters sometimes. DO you know the order I should be listing them? I tried Android_User's answer but it also force closed on me (I modified it and used ACTION_SEND_MULTIPLE). Thank You. – ThatGuyThere Mar 23 '13 at 07:13
  • Yes. You must substituted a String name, like this: "String myText = editText.getText().toString();". This is how you get the user's edited text from the 'EditText' widget so that you can send it in the email. – David Manpearl Mar 23 '13 at 15:36
  • This still crashes my application and I have no idea why. Here's a link to see my code. http://pastebin.com/BFiNuBQi – ThatGuyThere Mar 23 '13 at 19:40
  • You put both 'subjectdata' and 'emailmsg' on "EXTRA_SUBJECT". 'emailmsg' should go on "EXTRA_TEXT". Also, switch from 'startActivity()' to 'startActivityForResult()' if possible (requires an extra int 'resultCode' parameter) so that execution will return to your Activity's 'onActivityResult()' function after the message gets sent by the user. Unfortunately, I don't think either of these updates will fix your crash problem. For that, check the LogCat for line number of crash. Or, post the LogCat output (here or PasteBoard). – David Manpearl Mar 23 '13 at 21:14
  • Found your bug. Your 'findViewById()' function calls are all wrong. Change to "subjectline = (EditText) findViewById(R.id.subjectline); emailcontent = (EditText) findViewById(R.id.emailmessage);" – David Manpearl Mar 23 '13 at 21:20
  • What version of Android are you using? @doreamon (in this post "http://stackoverflow.com/questions/8701634/send-email-intent") says for 4.1.2 you must use this: "Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts( "mailto","abc@gmail.com", null));". – David Manpearl Mar 23 '13 at 21:28
  • okay, so I used the code given by @doreamon from that post and your fixes of my 'findViewById()' function calls. It doesn't crash but the option chooser says there is no app to perform the function when I have the gmail app installed. It also gives me an error on the lines where i say 'putExtra'. I'm running android 4.2.2. Here's my code. As you can see, I had to comment out the two lines (I put them before the createChooser() call and it still doesn't work. http://pastebin.com/zgtib9Vf – ThatGuyThere Mar 24 '13 at 07:34
  • 1
    Ok, you should put the 'new Intent' line back the way it was: "Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);", also try "ACTION_SENDTO". – David Manpearl Mar 24 '13 at 07:39
  • I've found the answer. Check my updated question. Thank you for all your help! – ThatGuyThere Mar 24 '13 at 08:25