-1

I'm trying to make a Button that changes to another activity, and at the same time creates a new textView with text from user input. I figured out how to do it on only one activity(I got it from this: LINK ). However, I want to do the exact same thing, except I want the button to take me to another activity and creates a textView there.

The point of this is to have an overview over accounts. You go to a "create account page", create an account, and the account shows up on the overview.

This is what I have so far:

First class:

public class MainActivity extends AppCompatActivity {

private EditText mEditText;
private Button mButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mEditText = (EditText) findViewById(R.id.editText);
    mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(onClick());

    TextView textView = new TextView(getApplicationContext());
    textView.setText("New text");

}

private View.OnClickListener onClick() {
    return new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent buttonIntent = new Intent(MainActivity.this, 
SecondLayout.class);
            MainActivity.this.startActivity(buttonIntent);

mLayout.addView(createNewTextView(mEditText.getText().toString()));

        }
    };
}

private TextView createNewTextView(String text) {
    final LinearLayout.LayoutParams lparams = new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
LinearLayout.LayoutParams.WRAP_CONTENT);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(lparams);
    textView.setText("New text: " + text);
    return textView;
}
}

Second class:

public class SecondLayout extends AppCompatActivity {

public static LinearLayout mLayout;


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

    mLayout = (LinearLayout) findViewById(R.id.linearLayout);


    final Button addAccountButton = (Button) 
findViewById(R.id.secondButton);
    addAccountButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent buttonIntent = new Intent(SecondLayout.this, 
MainActivity.class);
            SecondLayout.this.startActivity(buttonIntent);
        }
    });

}

}

XML of first activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/firstlayout">
<EditText
    android:id="@+id/firstText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button"
     />
</LinearLayout>

XML of second activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/firstlayout">
<EditText
    android:id="@+id/firstText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button"
     />
</LinearLayout>

I'm very new to android studio and java, so there's probably some obvious mistakes that I can't see.

Community
  • 1
  • 1
Mgulb
  • 19
  • 2

1 Answers1

0

You should not create the textview in firstactivity. Instead pass the text of the edittext as extras in the intent to the secondactivity. and fetch the string in second activity and create the textview in secondactivity.

Check how to pass string in intent

Pass a String from one Activity to another Activity in Android

Community
  • 1
  • 1
GeeekiGeek
  • 11
  • 2