Most android applications, atleast serious ones, comprise more than one activity. Activities in android User interface units. We need a way of then passing data from one activity to another.Thats this lessons aim. The aim is to see how to pass Strings,Integers and Booleans, primitive data types from one activity to another using Intent.Here's the thing :

  • Pass integers,booleans and string from one activity to another.
  • The booleans shall be represented in a checkbox,the string in an edittext while the integer also in an edittext.
  • The user enters them in our MainActivity,we pass them via intent, and show them in second activity.
  • We use intents.

Android Pass String Integer and Boolean from Activity to another  

Create/Download Project

  • Download the project above.
  • You'll get a zipped file,extract it.
  • Open the Android Studio.
  • Now close, already open project
  • From the Menu bar click on File >New> Import Project
  • Now Choose a Destination Folder, from where you want to import project.
  • Choose an Android Project.
  • Now Click on “OK“.
  • Done, your done importing the project,now edit it.

Our Layouts

ContentMain.xml

  • Our MainActivity's layout.
  • Add Checkbox to show boolean values and edittexts to show integer and string.
  • User enters these values at runtime.

   

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.tutorials.hp.primitivespassing.MainActivity"
    tools:showIn="@layout/activity_main">

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

        <android.support.design.widget.TextInputEditText
            android:id="@+id/nameTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:enabled="true"
            android:focusable="true"
            android:hint="Name"
            android:textSize="25dp"
            android:textStyle="bold" />

        <android.support.design.widget.TextInputEditText
            android:id="@+id/txtID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:enabled="true"
            android:focusable="true"
            android:hint="ID"
            android:textSize="25dp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:text="Technology Exists ??"
                android:textSize="25dp"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/techExists"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:textSize="25dp" />
        </LinearLayout>
        <Button android:id="@+id/sendBtn"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:text="Send"
            android:clickable="true"
            android:padding="5dp"
            android:background="#009968"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="20dp" />
    </LinearLayout>
</RelativeLayout>

       

ActivitySecond.xml

  • Our SecondActivity's layout.
  • Add Checkbox to show boolean values and edittexts to show integer and string.

 

<?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.tutorials.hp.primitivespassing.SecondActivity">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:text="NAME"
                android:textSize="25dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/nameTxtSecond"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="value received"
                android:textSize="25dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:text="ID"
                android:textSize="25dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txtIDSecond"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="value received"
                android:textSize="25dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:text="Technology Exists ??"
                android:textSize="25dp"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/techExistsSecond"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:textSize="25dp" />
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

     

Our Classes

MainActivity class

  • Launcher activity.
  • We pack data into an intent and pass them to Second activity from here.

 

package com.tutorials.hp.primitivespassing;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {

    //DECLARE VIEWS
    private TextInputEditText txtName, txtID;
    private CheckBox chkTechnologyExists;
    private Button sendBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        this.initializeViews();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        //WHEN SEND BTN IS CLICKED,SEND
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendData();
            }
        });
    }

    /*
    REFERENCE VIEWS WE ARE USING
     */
    private void initializeViews() {
        txtName = (TextInputEditText) findViewById(R.id.nameTxt);
        txtID = (TextInputEditText) findViewById(R.id.txtID);
        chkTechnologyExists = (CheckBox) findViewById(R.id.techExists);
        sendBtn = (Button) findViewById(R.id.sendBtn);

    }

    /*
    SEND DATA TO SECOND ACTIVITY
     */
    private void sendData() {
        //GET PRIMITIVE VALUES TO SEND
        String name = txtName.getText().toString();
        int id = Integer.parseInt(txtID.getText().toString());
        Boolean techExists = chkTechnologyExists.isChecked();

        //PACK THEM IN AN INTENT OBJECT
        Intent i = new Intent(this, SecondActivity.class);
        i.putExtra("NAME_KEY", name);
        i.putExtra("ID_KEY", id);
        i.putExtra("TECHEXISTS_KEY", techExists);

        //LETS LEAVE OUR TXTS CLEARED
        txtName.setText("");
        txtID.setText("");

        //START SECOND ACTIVITY
        this.startActivity(i);
    }

}

     

SecondActivity class

  • This activity unpacks the data sent by intent from MainActivity.
  • It then displays them in their respective views.

 

package com.tutorials.hp.primitivespassing;

import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {

    //DECALRE SECOND ACTIVITY VIEWS
    TextView txtName2;
    TextView txtID2;
    CheckBox chkTechnologyExists2;

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

        //INITIALIZE THESE VIEWS
        txtName2 = (TextView) findViewById(R.id.nameTxtSecond);
        txtID2 = (TextView) findViewById(R.id.txtIDSecond);
        chkTechnologyExists2 = (CheckBox) findViewById(R.id.techExistsSecond);

        //RECEIVE DATA FROM MAIN ACTIVITY
        String name = getIntent().getStringExtra("NAME_KEY");
        int id = getIntent().getIntExtra("ID_KEY", 0);
        Boolean techExists = getIntent().getBooleanExtra("TECHEXISTS_KEY", false);

        //SHOW A TOAST
        Toast.makeText(SecondActivity.this, name, Toast.LENGTH_LONG).show();

        //SET THE DATA TO OUR LOCAL VIEWS
        txtName2.setText(name);
        txtID2.setText(String.valueOf(id));
        chkTechnologyExists2.setChecked(techExists);
    }
}

More Resources:

Resource Link
GitHub Browse Browse
GitHub Download Link Download

Oclemy,Cheers.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post