Pass value from one Activity to another in Android  

when you want to send data from one Activity to another activity then you can use Extras method ( putExtras() ) of Intent to send data and getStringExtra to get . That data can be in the form of String, Integer, Serialized object etc.

0 374 Technology | Android App
Sat Nov 10 , 2018

Step 1 : Create a new project name it "SendValueExample"

Step 2 : Select Empty Activity click ok.

Step 3 : Open main_activity.xml

Copy and paste code


	<EditText
        android:id="@+id/edittext_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Your Name"
        android:textColor="#CCCCCC"
        android:textColorHint="#cccccc"
        />

    <EditText
        android:id="@+id/edittext_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Your Age"
        android:textColor="#CCCCCC"
        android:textColorHint="#cccccc"
        />
    <Button
        android:id="@+id/btn_sendvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Value"
        />

Step 4 : Now Open MainActivity.java

Copy and paste


    EditText Name,Age;
    Button SendValue;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_step_five);


        Name=findViewById(R.id.edittext_name);
        Age=findViewById(R.id.edittext_age);
        SendValue=findViewById(R.id.btn_sendvalue);
        


        SendValue.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {

               intent = new Intent(StepFive.this,RetriveValue.class);
               intent.putExtra("message","Hello "+Name.getText().toString()+" Your Age is :"+Age.getText().toString());
               startActivity(intent);

           }
        });

    }

Step 4 : Now Add One new Activity name it RetriveValue

Step 5 :Open retrivevalue.xml

Copy and paste


TextView
        android:id="@+id/textview_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginBottom="10sp"
        android:textColor="#665858"
        />

Step 6 : Open RetriveValue.java

Copy and paste


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


        SetValue = (TextView)findViewById(R.id.textview_value);
        Intent intent=getIntent();
        String message=intent.getStringExtra("message");
        SetValue.setText(message);

    }

Build the project

Run


Subscribe & Get Regular Updates On Your E-mail