user1106352
user1106352

Reputation:

trying to get string into intent

Ok I am trying to take a string and put it into the intent section so i can get the app to send an email. I cannot for the life of me figure out how to do this. NOTE: I am new to java/android programming. I know this will pull up an option for the user to choose what they want to send it with. I would really like to have to app send the email and give them a confirmation, but do not know if that is possible.

Here is my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/welcome" />

    <EditText
        android:id="@+id/EditTextName"
        android:layout_height="wrap_content"
        android:hint="@string/name"
        android:inputType="textPersonName"
        android:layout_width="fill_parent">
    </EditText>

    <EditText
        android:id="@+id/EditTextEmail"
        android:layout_height="wrap_content"
        android:hint="@string/email"
        android:inputType="textEmailAddress"
        android:layout_width="fill_parent">
    </EditText>

    <Spinner
        android:id="@+id/SpinnerStatus"
        android:layout_height="wrap_content"
        android:prompt="@string/status"
        android:layout_width="fill_parent"
        android:entries="@array/statuslist">
    </Spinner>

    <EditText  
        android:id="@+id/EditTextChangeBody"  
        android:layout_height="wrap_content"  
        android:hint="@string/changebody"  
        android:inputType="textMultiLine"  
        android:lines="5"  
        android:layout_width="fill_parent">  
    </EditText>

    <Button
        android:id="@+id/ButtonSendChange"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="sendChange"
        android:text="@string/changebutton">
    </Button>

</LinearLayout>

Here is my MadisonStudios.java

package com.madisonstudios.supportapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

public class MadisonStudios extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void sendChange(View button) {  
        final EditText nameField = (EditText) findViewById(R.id.EditTextName);  
        String name = nameField.getText().toString();  

        final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);  
        String email = emailField.getText().toString();  

        final EditText feedbackField = (EditText) findViewById(R.id.EditTextChangeBody);  
        String changes = feedbackField.getText().toString();

        final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerStatus);  
        String status = feedbackSpinner.getSelectedItem().toString();

        Intent it = new Intent(Intent.ACTION_SEND);
        String[] tos = {getString(R.string.email)};
        it.putExtra(Intent.EXTRA_EMAIL, tos);
        it.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.emailSubject));
        it.setType("text/plain");
        startActivity(it);
    } 
}

Upvotes: 1

Views: 926

Answers (1)

rfsk2010
rfsk2010

Reputation: 8611

You can access getString() from a context or its subclass like activity,

to get a string you need to do the following

activity.getString(R.string.xyz)

You can pass a string to another activity like the following code does.

    final Intent activityIntent = new Intent(
                activity.getApplicationContext(), CurrentActivity.class);
        activityIntent .putExtra("ID",
                item.ID);
        activityIntent .putExtra("NAME",
                item.name);
        activity.startActivity(activityIntent);

You can send an email using the following code

public static void sendEmail(final Activity activity,final String email) {

    /* Create the Intent */
    final Intent emailIntent = new Intent(
            android.content.Intent.ACTION_SEND);

    /* Fill it with Data */
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
            new String[] {email });
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "Feedback");

    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
    /* Send it off to the Activity-Chooser */
    activity.startActivity(Intent.createChooser(emailIntent,
            "Send feedback"));
}

Upvotes: 2

Related Questions