0

I have a String value which is the text from an EditText and I want to use that String value in another activity. In a TextView the user puts his/her name and in the next activity I want to have a Welcome screen that says, Hello, name

package com.example.aprendelastablasdemultiplicar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class pantalla2 extends AppCompatActivity {
    private EditText ingresarnombre;
    private TextView cifracero;
    private TextView cifrauno;
    ingresarnombre =         (EditText)findViewById(R.id.ingresarnombre);
    String nombre = ingresarnombre.getText().toString();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pantalla2);
        cifracero.setText(0);
        cifrauno.setText(1);
    }
}
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Mauro Stancato
  • 537
  • 1
  • 9
  • 19
  • check this : https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities – Mohammad Sianaki Oct 29 '19 at 00:50
  • You can use Bundle data or to keep persistent use SharedPreferences. – Sachin Bahukhandi Oct 29 '19 at 03:54
  • you can pass data from one activity to another by using intent or use SharedPreferences for saving the data for the entire application life time – Sharon Joshi Oct 29 '19 at 04:12
  • 1
    Does this answer your question? [What's the best way to share data between activities?](https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities) – Ryan M Apr 17 '21 at 06:24

2 Answers2

3

In your first activity where you will put the name on the edittext, just get the string from the Edittext and pass that with intent.

FirstActivity :

public class FirstActivity extends AppCompatActivity {

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

        EditText edittext = (EditText)findViewById(R.id.edittext);
        String name = edittext.getText().toString();

        Intent intent = new Intent(ActivityOne.this,ActivityTwo.class);
        intent.putExtra("name_extra",name);

        startActivity(intent);

    }
}

On your second activity just receive the string extra value from intent and work with it.

Second Activity :

public class SecondActivity extends AppCompatActivity {

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

    String name = getIntent().getStringExtra("name_extra");

    TextView textView = (TextView) findViewById(R.id.textView);

    textView.setText("Welcome "+name);

    }
}
0

Create a button in your pantalla2 so that on button click you can go to another activity.And make sure you have given the id of your textview and button in activity_pantalla2 See this it will work:

       public class pantalla2 extends AppCompatActivity {
        private EditText ingresarnombre;
        private Button button;
        private TextView cifracero;

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

            cifracero = findViewById(R.id.txtCifracero);
            button = findViewById(R.id.button);

            String cifracero = cifracero.getText().toString().trim();

            button.setOnClickListener(new View.OnClickListener() { 
              @Override
              public void onClick(View view) 
              { 
                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                intent.putExtra("key", cifracero);
                startActivity(intent);
              } 
             });  
       }
}

In your SecondActivity you just have to add textview in your layout and get the value send from first activity.

   public class SecondActivity extends AppCompatActivity {

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

    String name = getIntent().getStringExtra("name_extra");

    TextView textView = (TextView) findViewById(R.id.textView);

    textView.setText("Welcome "+name);

    }
}