DEV Community

Cover image for How To Pass Data To Another Activity
Anoop Patel
Anoop Patel

Posted on

How To Pass Data To Another Activity

there are two activities

  • MainActivity.java

  • SettingActivity.java

MainActivity.java

 public void launchSettings(View v){

        //Launch a new activity

        Intent i = new Intent(this,SettingActivity.class);
        String message = ((EditText)findViewById(R.id.editTextText)).getText().toString();
        i.putExtra("cool", message);
        startActivity(i);
    }
Enter fullscreen mode Exit fullscreen mode

SettingActivity.java

public class SettingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_setting);

        Intent i = getIntent();
        String message = i.getStringExtra("cool");
        TextView t = findViewById(R.id.textview);
        t.setText(message);
    }
}
Enter fullscreen mode Exit fullscreen mode

There are XMl Files

1.activity_main.xl


<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editTextText"
        android:layout_width="470dp"
        android:layout_height="64dp"
        android:ems="10"
        android:inputType="text"
        android:text="Name"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="7dp"
        tools:layout_editor_absoluteY="30dp" />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="launchSettings"
        android:text="button"
        app:layout_constraintEnd_toEndOf="parent"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteY="125dp" />
</LinearLayout>

Enter fullscreen mode Exit fullscreen mode

2.activitysetting.xml

 <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="50sp"
        android:text="hello activity"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.224"
        tools:layout_editor_absoluteX="139dp"
        tools:ignore="MissingConstraints" />
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay