Passing Parameters between Activities

Apps can contain more than one Activity, in order to communicate with Activities pass parameters (messages) with each other in the following way. This example contains two activities :

  1. CreateMessageActivity (Initial activity that passes messages to ReceiveMessageActivity Activity)
  2. ReceiveMessageActivity (Get passed messages from CreateMessageActivity and displays them)

CreateMessageActivity.java

package com.example.admin.messenger;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_message);
        Intent intent = getIntent();

        Bundle extras = getIntent().getExtras();
        String usersNameText = extras.getString("USERS_NAME");
        String usersPasswordText = extras.getString("USERS_PASSWORD");

        String finalMessage = usersNameText + " " + usersPasswordText;
        TextView messageView = (TextView) findViewById(R.id.message);

        messageView.setText(finalMessage);

    }
}

ReceiveMessageActivity.java

package com.example.admin.messenger;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_message);
        Intent intent = getIntent();

        Bundle extras = getIntent().getExtras();
        String usersNameText = extras.getString("USERS_NAME");
        String usersPasswordText = extras.getString("USERS_PASSWORD");

        String finalMessage = usersNameText + " " + usersPasswordText;
        TextView messageView = (TextView) findViewById(R.id.message);

        messageView.setText(finalMessage);

    }
}

Design of create message activity (activity_create_message.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.admin.messenger.CreateMessageActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/labelForUsername"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="21dp" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/usersname"
        android:layout_alignBottom="@+id/textView"
        android:layout_toRightOf="@+id/textView"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/labelForPassword"
        android:id="@+id/textView2"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/textView"
        android:layout_toLeftOf="@+id/usersname"
        android:layout_toStartOf="@+id/usersname" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/send"
        android:id="@+id/button"
        android:onClick="onSendMessage"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="62dp"
        android:layout_alignTop="@+id/textView2" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/userspassword"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/usersname"
        android:layout_alignStart="@+id/usersname"
        android:layout_alignRight="@+id/usersname"
        android:layout_alignEnd="@+id/usersname" />


</RelativeLayout>

Design of receive message activity (activity_receive_message.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.admin.messenger.ReceiveMessageActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/message"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

strings.xml (for layout labels)

<resources>
    <string name="app_name">Messenger</string>
    <string name="send">Login</string>
    <string name="labelForUsername">Username</string>
    <string name="labelForPassword">Password</string>
</resources>

Output

Directory Structure of the Project

untitled

Share

You may also like...