Sending Information in Android

Introduction

In this article I will explain how to send some comments or messages to another activity in Android. In this article I want to send my message to an another activity that is a login activity of another company. To understand how to do that see the following instructions.

Step 1

Create a new Android project as "File" ->"New" -> "Android Application Project" as shown in the following image:

newActivity.jpg

Step 2

Now open the XML file as "res/layout/activity_main.xml" and update it with the following code:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <TextView android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="Name : "/>

    <EditText android:id="@+id/name"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_marginBottom="10dip"/>

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Email : "

        />

    <EditText android:id="@+id/email"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_marginBottom="10dip"/>

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Mobile :" />

    <EditText

        android:id="@+id/mobile"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:ems="10"

        android:maxLength="10"

        android:inputType="phone" >

        <requestFocus />

    </EditText>

    <TextView       

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Comment :" />

    <EditText

        android:id="@+id/comment"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:ems="10" />

    <Button

        android:id="@+id/btnNextScreen"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="15dip"

        android:text="Connect to Next" />

</LinearLayout>

Step 3

Open the Java file as "MainActivity.java" and update it with the following code:

package com.example.activityswitching;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity {

        EditText inputName, inputMobile;

          EditText inputEmail,inputComment;

          @Override

          public void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main);

              inputName = (EditText) findViewById(R.id.name);

              inputEmail = (EditText) findViewById(R.id.email);

              inputMobile = (EditText) findViewById(R.id.mobile);

              inputComment = (EditText) findViewById(R.id.comment);

              Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);

              //Listening to button event

              btnNextScreen.setOnClickListener(new View.OnClickListener() {

                  public void onClick(View arg0) {

                      //Starting a new Intent

                      Intent nextScreen = new Intent(getApplicationContext(), SecondActivity.class);

                      //Sending data to another Activity

                      nextScreen.putExtra("name", inputName.getText().toString());

                      nextScreen.putExtra("email", inputEmail.getText().toString());

                      nextScreen.putExtra("mobile", inputMobile.getText().toString());

                      nextScreen.putExtra("comment", inputComment.getText().toString());

                      Log.e("n", inputName.getText()+"."+ inputEmail.getText()+"."+inputMobile.getText()+"."+inputComment.getText());

                      startActivity(nextScreen);

                  }

              });

          }

      }

Step 4

Now create a new XML file in "res/layout/second_activity.xml" and update it with the following code:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="match_parent"

  android:layout_height="match_parent">

  <TextView android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="You Entered..."

            android:textSize="25dip"

            android:gravity="center"

            android:layout_margin="15dip"/>

  <TextView android:id="@+id/txtName"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_margin="15dip"

            android:textSize="18dip"/>

  <TextView android:id="@+id/txtEmail"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_margin="15dip"

            android:textSize="18dip"/>

  <TextView

      android:id="@+id/txtmobile"

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:layout_margin="15dip"

     />

  <TextView

      android:id="@+id/txtcomment"

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:layout_margin="15dip"

 />

  <Button android:id="@+id/btnClose"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="15dip"

            android:text="Close"/>

</LinearLayout>

Step 5

Create a new Java file as "src/com.example.activityswitching/SecondActivity.java" and update it with the following code:

package com.example.activityswitching;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class SecondActivity extends Activity {

       public void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_second);

              TextView txtName = (TextView) findViewById(R.id.txtName);

              TextView txtEmail = (TextView) findViewById(R.id.txtEmail);

              TextView txtmobile = (TextView) findViewById(R.id.txtmobile);

              TextView txtcomment = (TextView) findViewById(R.id.txtcomment);

              Button btnClose = (Button) findViewById(R.id.btnClose);

              Intent i = getIntent();

              // Receiving the Data

              String name = i.getStringExtra("name");

              String email = i.getStringExtra("email");

              String mobile = i.getStringExtra("mobile");

              String comment = i.getStringExtra("comment");

              //Log.e("Second Screen", name + "." + email);

              // Displaying Received data

              txtName.setText(name);

              txtEmail.setText(email);

              txtmobile.setText(mobile);

              txtcomment.setText(comment);

              // Binding Click event to Button

              btnClose.setOnClickListener(new View.OnClickListener() {

                  public void onClick(View arg0) {

                      //Closing SecondScreen Activity

                      finish();

                  }

              });

          }

      }

Step 6


The output is as shown in the following image:

Input

connectionActivity.jpg

Output

resultActivity.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all