ThatGuyThere
ThatGuyThere

Reputation: 365

Pass info from edittext through an email intent

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


    }

};

Upvotes: 0

Views: 3309

Answers (2)

Ajay
Ajay

Reputation: 1189

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[]{"[email protected]"});
email.putExtra(Intent.EXTRA_TEXT, shareddata);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Send Mail :"));

Upvotes: 1

David Manpearl
David Manpearl

Reputation: 12656

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

Upvotes: 0

Related Questions