-3

I have been pulling my hair out for the last 4 hours trying to find out how to do this.

I have checked SO for other questions but found nothing good. I am an intermediate programmer and would be very appreciative if your comments and answers were written in a way that I can understand.

I want to be able to take the text that the user enters into the @+id/location Edittext box on the InputPage activity and display it in the @+id/ListName TextView on the InputPage2 activity when the button on the InputPage activity is pressed.

InputPage java code

public class InputPage extends Activity {
    public String name;
    public InputPage() {
}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_page );
        Button button = (Button)findViewById(R.id.addButton);
    }


    public InputPage(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public String name(String name){
        this.name = name;
        EditText editText = (EditText)findViewById(R.id.location);
        String value = editText.getText().toString();
        return value;

    }

    public void toInputScreen2(View view) {
        Button button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(InputPage.this, InputPage2.class);
                startActivity(intent2);
            }
        });
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".InputPage"
    android:background="@color/BackgroundGrey">

    <TextView
        android:id="@+id/mapNo"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="@string/mapNoTextBox"
        android:textColor="@color/textColour"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.04"
        app:layout_constraintVertical_bias="0.13" />

    <TextView
        android:id="@+id/inputPageTitle"
        android:layout_width="178dp"
        android:layout_height="21dp"
        android:text="@string/inputPageTitle"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@color/textColour"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.01999998" />

    <EditText
        android:id="@+id/noInput"
        android:layout_width="48dp"
        android:layout_height="39dp"
        android:ems="10"
        android:inputType="number"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.13"
        app:layout_constraintHorizontal_bias="0.51" />

    <TextView
        android:layout_width="150dp"
        android:layout_height="38dp"
        android:text="@string/location"
        android:textColor="@color/textColour"
        android:textSize="30sp"
        android:textStyle="bold"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintHorizontal_bias="0.041"
        app:layout_constraintVertical_bias="0.24" />

    <EditText
        android:id="@+id/location"
        android:layout_width="177dp"
        android:layout_height="44dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="@string/hintText"
        android:textColorHint="@color/hintTextColour"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.952"
        app:layout_constraintVertical_bias="0.243" />

    <EditText
        android:id="@+id/dateBox"
        android:layout_width="133dp"
        android:layout_height="44dp"
        android:ems="10"
        android:inputType="date"
        android:hint="@string/dateBoxHint"
        android:textColorHint="@color/hintTextColour"
        android:layout_marginLeft="61dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.335"
        app:layout_constraintVertical_bias="0.374" />

    <TextView
        android:layout_width="101dp"
        android:layout_height="34dp"
        android:text="@string/dateBox"
        android:textColor="@color/textColour"
        android:textStyle="bold"
        android:textSize="30sp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.032"
        app:layout_constraintVertical_bias="0.369" />

    <Button
        android:id="@+id/button2"
        android:layout_width="165dp"
        android:layout_height="58dp"
        android:text="@string/continueButton"
        android:background="@drawable/buttons"
        android:layout_marginRight="8dp"
        android:onClick="toInputScreen2"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintVertical_bias="0.778" />


</android.support.constraint.ConstraintLayout>

**InputPage2 java code **

public class InputPage2 extends Activity  {

    Button saveButton1;
    EditText message;
    String Message;
    TextView[] pairs;
    int num_match;




    public TextView[] getPairs() {
        return pairs;
    }

    public void setPairs(TextView[] pairs) {
        this.pairs = pairs;
    }

    public int getNum_match() {
        return num_match;
    }

    public void setNum_match(int num_match) {
        this.num_match = num_match;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_page2);
        message = (EditText) findViewById(R.id.textNotAtHomes);



    }

    public void toSaveField(View v) {
        saveButton1 = (Button) findViewById(R.id.saveButton1);
        saveButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }

        });


        Message = message.getText().toString();
        try {
            FileOutputStream fos = openFileOutput("Text.txt", MODE_WORLD_READABLE);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            try {
                osw.write(Message);
                osw.flush();
                osw.close();
                Toast.makeText(getBaseContext(), "Saved successfully", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Intent intent2 = new Intent(InputPage2.this, MainActivity.class);
        startActivity(intent2);
    }

    public void linearLayout(TextView[] pairs, int num_match) {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        pairs = new TextView[num_match + 1];
        for (int l = 1; l <= num_match; l++) {
            pairs[1].setTextSize(15);
            pairs[1].setLayoutParams(layoutParams);
            pairs[1].setId(l);
            linearLayout.addView(pairs[1]);

        }
    }
}

associated XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".InputPage2"
    android:background="#ffffff"
    tools:layout_editor_absoluteY="25dp"
    tools:layout_editor_absoluteX="0dp"
    android:backgroundTint="@color/BackgroundGrey">


    <Button
        android:onClick="toSaveField"
        android:id="@+id/saveButton1"
        android:layout_width="341dp"
        android:layout_height="70dp"
        android:background="@drawable/buttons"
        android:text="@string/saveButton"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.666"
        app:layout_constraintVertical_bias="1.0"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />


    <EditText
        android:id="@+id/textNotAtHomes"
        android:layout_width="346dp"
        android:layout_height="477dp"
        android:ems="10"
        android:background="@color/hintTextColour"
        android:inputType="textMultiLine"
        android:hint="@string/hintText2"
        android:textColorHint="@color/hintTextColour2"
        android:layout_marginLeft="8dp"
        android:gravity="fill_horizontal"
        android:padding="6dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintVertical_bias="0.303" />

    <TextView
        android:id="@+id/ListName"
        android:layout_width="344dp"
        android:layout_height="37dp"
        android:textColor="@color/blackText"
        android:background="@color/whiteBackground"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintVertical_bias="0.0" />


</android.support.constraint.ConstraintLayout>

Any help is appreciated .

  • Possible duplicate of [How to send an object from one Android Activity to another using Intents?](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – Lingeshwaran May 22 '17 at 04:34

1 Answers1

2

You can pass data between activities like this

Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);

and get it with the other activity

 Bundle bundle = getIntent().getExtras();
 String message = bundle.getString("message");
Juliyanage Silva
  • 2,529
  • 1
  • 21
  • 33
  • @The Abstract Lightbulb since you are new..you need to pinpoint the exact thing you need to do..getting the data and showing is ok to you. but the real problem you have is how to pass data from one activity to another. – Juliyanage Silva May 22 '17 at 04:38
  • Thank you man. At least you actually tried to help me with my question. Really appreciate it. – The Abstract Lightbulb May 22 '17 at 06:10
  • @The Abstract Lightbulb,If this works for you, you can accept my question. – Juliyanage Silva May 22 '17 at 06:14
  • Sorry everybody. My question has had some editing done to it, so thanks to whoever did the editing it might be easier to understand. – The Abstract Lightbulb May 22 '17 at 06:48
  • Hi @Shamitha Silva. I tried what you suggested and it was able to get the message from one class to another and then i used the following syntax to try and display the message. `TextView txt1 =(TextView)findViewById(R.id.ListName);` ` txt1.setText(name);`. But when i tried to run my activity i get this error. – The Abstract Lightbulb May 22 '17 at 08:44
  • `java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.cohen.notathomesapp/com.example.cohen.not_at_homes_app.InputPage2}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference` – The Abstract Lightbulb May 22 '17 at 08:49
  • Got any ideas on why this is happening? – The Abstract Lightbulb May 22 '17 at 08:50
  • You need to receive the message inside onCreate() or after it and not before that. Because getExtras() needs a context – Juliyanage Silva May 22 '17 at 08:54