Home > database >  Put EditText in First Activity to TextView on Second Activity
Put EditText in First Activity to TextView on Second Activity

Time:11-22

I'm french so sorry for my bad english.

I need to update on my second activity my TextView with a EditText of my first Activity. But I don't how to do.

That is my code on First Activity:

public class MainActivity extends AppCompatActivity {

private TextView mGreetingTextView;
private EditText mLoginEditText,mEmailEditText,mPasswordEditText;
private Button mLoginButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGreetingTextView = findViewById(R.id.main_textview_info);
    mLoginEditText = findViewById(R.id.main_edittext_login);
    mEmailEditText = findViewById(R.id.main_edittext_email);
    mPasswordEditText = findViewById(R.id.main_edittext_password);
    mLoginButton = findViewById(R.id.main_button_log);

    mLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            validateLogin();
        }
        String login = mLoginEditText.getText().toString();
    });
}
private void validateLogin(){
    if (mEmailEditText.getText().toString().equals("[email protected]") &&
            mPasswordEditText.getText().toString().equals("admin")) {
        Toast.makeText(getApplicationContext(), "Login successful",Toast.LENGTH_SHORT).show();
        callHome();
    } else {
        Toast.makeText(getApplicationContext(), "Wrong login",Toast.LENGTH_SHORT).show();
    }
}
public void callHome(){
    Intent i = new Intent(getApplicationContext(),HomeActivity.class);
    i.putExtra("mLoginText", mLoginEditText.getText());
    startActivity(i);
}

But I search a same topic who has the same problem and I don't find. Apologize for my bad level on Android but I'm student on Android. This is my first topic on StackOverFlow ^^

CodePudding user response:

You are calling mLoginEditText.getText() and that returns Editable. You will need to call mLoginEditText.getText().toString() and that will be a String.

CodePudding user response:

You can update your second activity (HomeActivity) like this

public class HomeActivity extends AppCompatActivity {

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    textView = findViewById(R.id.textView);

    // get the text from main activity
    Intent intent = getIntent();
    String text = intent.getStringExtra("mLoginText");
    textView.setText(text);
}}
  • Related