Passing Data Between Activities in Android Application— For Beginners

Prerequisites

  • Android Studio (Download Link)
  • Basic knowledge of XML and Java
  • Android Emulator
<resources>
<string name="app_name">Activity2</string>
<string name="message_txt">Enter Your message</string>
<string name="send_button_text">Send</string>
<string name="title_activity_second">SecondActivity</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/message_text"
android:hint="@string/message_txt"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/send_button_text"
android:onClick="sendmessage"
/>
</LinearLayout>
package com.example.activity2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText message_text;

public final static String
MESSAGE_KEY ="com.example.message_key";

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

public void sendmessage (View view)
{
message_text = (EditText) findViewById(R.id.message_text);

String message = message_text.getText().toString();

Intent intent= new Intent(this ,SecondActivity.class);

intent.putExtra(MESSAGE_KEY,message);

startActivity(intent);
}}
package com.example.activity2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {

public final static String MESSAGE_KEY ="com.example.message_key";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(45);
textView.setText(message);
setContentView(textView);

}

}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.activity2.SecondActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_second" />

</android.support.design.widget.CoordinatorLayout>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store