Marc Karam
Marc Karam

Reputation: 465

Passing data from one view to another using intents

I know there are other questions that have already been asked on this topic and I've already looked through a lot of them and tried their answers but none of them have worked.

I've got 2 activities, MainActivity and Main2Activity (that was the given name) (I'll refer to them as 1 and 2 respectively) each with their own respective XML files. What I'm trying to do is have a user enter something into an EditText field on 2 and then when they press enter, what they entered will appear in a TextView on 1. I'm trying to first get it to work when they press a button but it refuses to work. The ultimate goal is for it to get it to get transferred to 1 when they press/tap the enter key on the keyboard that pops up, so if it's easier to just skip the button and go straight to the enter key then by all means.

Any help that I can get is greatly appreciated. I'll put my (relevant) code as well as a couple screenshots below. Thank you.

Java code for 2 (Main2Activity):

public class Main2Activity extends AppCompatActivity {

    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        et = (EditText) findViewById(R.id.editText);
    }

    EditText text;

    public void CLICK_THIS (View v)
    {
        Intent intent = new Intent();
        intent.putExtra("TextValue", et.getText().toString());
        intent.setClass(Main2Activity.this, MainActivity.class);
        startActivity(intent);
    }

    Intent intent = getIntent(); //i honestly forgot what this line is doing here
//i think it's here from some other thing i was trying to do and i just forgot to delete it.
}

Java code for 1 (MainActivity):

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        view = (ImageView) findViewById(R.id.imageView3);
        backgroundImageName = String.valueOf(view.getTag());

        TextView text = (TextView) findViewById(R.id.textView2);
        String s = getIntent().getStringExtra("TextValuie");
        text.setText(s);
    }

Screenshot 1 (this is what MainActivity (1) looks like):

The text that's highlighted is just to show where the TextView is. it's "not supposed to be here". image 1

Screenshot 2 (this is what Main2Activity (2) looks like):

image 2

Screenshot 3 (this just shows the keyboard) (ultimately the user would type something into the "Enter Your Text Here" and then press the Check mark and it would appear where the highlighted text is in 1 instead of clicking the button in 2 for that to happen). If I've confused anyone at any point please don't hesitate to ask for more information.

image 3

Upvotes: 0

Views: 49

Answers (1)

Prens
Prens

Reputation: 420

  • First of all you are retrieving the input using a wrong label "TextValuie" instead of "TextValue"
  • The other thing is that are you entering the text that on Button Click the EditText can get the entered value or the default would be an empty string?

You can always debug and put a break point on the intent to see the data that you are getting back from the activity.

Upvotes: 1

Related Questions