top of page

package com.mycompany.twoactivities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    //because we need to use variables in more then on place declare them.
    //Define input fields
    EditText editTextFirstName,editTextLastName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        editTextFirstName = (EditText) findViewById(R.id.editTextFirstName);
        editTextLastName = (EditText) findViewById(R.id.editTextLastName);
// buttonOne is only used here, so only declared here as a Button.
        Button buttonOne = (Button) findViewById(R.id.button1);
        // assign buttonOne a listener
        buttonOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*the manifest-file of our application and will display an
                Activity if it finds one with the same class. ie ActivityTwo.class*/
                Intent intent = new Intent(getApplicationContext(),ActivityTwo.class);
                //"fname" and "lname" must be used again in ActivityTwo so remember them.
                //Extract String objects with names fname and lname
                intent.putExtra("fname", editTextFirstName.getText().toString());
                intent.putExtra("lname", editTextLastName.getText().toString());

                /*the putExtra method is used it adds a key-value pair to the object.
                First parameter is key(name), the second - value.*/
//we then send Intent using the startActivity method
                startActivity(intent);
            }
        });
    }
//    Replaced this code with the new code
 /*   buttonOne.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
//                notice ActivityTwo is what we want to start
                Intent intent = new Intent(getApplicationContext(),ActivityTwo.class);
//                use startActivity() to start the new activity described by the intent
                startActivity(intent);

            }}*/
   }
 

MainActivity.java
ActivityTwo.java

package com.mycompany.twoactivities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class ActivityTwo extends AppCompatActivity {

    TextView theView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
//Find TextView first
        theView = (TextView) findViewById(R.id.theView);
        Intent intent = getIntent();
//receive Intent and extract String objects
        // "fname" and "lname"
        String fName = intent.getStringExtra("fname");
        String lName = intent.getStringExtra("lname");

        theView.setText("Your name is: " + fName + " " + lName);
    }



    public void myClick(View v)
    {
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
    }
}
 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mycompany.twoactivities.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Primary"
        android:id="@+id/primary" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:id="@+id/button1"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/editTextFirstName"
        android:layout_below="@+id/primary"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:hint="First Name" />

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/editTextLastName"
        android:layout_below="@+id/editTextFirstName"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:hint="Last Name" />

</RelativeLayout>
 

activity_two.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mycompany.twoactivities.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Secondary"
        android:id="@+id/second" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:id="@+id/button2"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="myClick"/>

    <TextView
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="First and Last Name"
        android:id="@+id/theView"
        android:layout_below="@+id/second"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="65dp" />
</RelativeLayout>
 

bottom of page