Mauro Stancato
Mauro Stancato

Reputation: 587

How to read a value from a String from a java file to another file?

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);
    }
}

Upvotes: 0

Views: 123

Answers (2)

Gourav Uniyal
Gourav Uniyal

Reputation: 16

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);

    }
}

Upvotes: 0

Sorwar Hossain Mostafa
Sorwar Hossain Mostafa

Reputation: 255

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);

    }
}

Upvotes: 3

Related Questions