Android Activity → Android Activities – Pass Primitives From One Activity To Another – Camposha

Android Activity

Android Activity

Android Activities – Pass Primitives From One Activity To Another

Let’s see how to pass primitive data types from one activity to another. We pass:

  • Strings
  • Integer
  • Boolean(Via CheckBox)

First Activity

First Activity

Second Activity
Second Activity

to a second activity and show them in the second activity.

Gradle Files

We’ll add dependencies in the app level build.gradle file.

1. Build.gradle

Here’s our app level in the build.gradle file:

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 24
        buildToolsVersion "25.0.1"

        defaultConfig {
            applicationId "com.tutorials.hp.primitivespassing"
            minSdkVersion 15
            targetSdkVersion 24
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:24.2.1'
        compile 'com.android.support:design:24.2.1'
    }

LAYOUT RESOURCES

We have three xml layouts;

  1. activity_main.xml
  2. content_main.xml
  3. activity_second.xml

1. activity_main.xml

  • The template layout for our main activity.
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
        
        
        android_layout_width="match_parent"
        android_layout_height="match_parent"
        android_fitsSystemWindows="true"
        tools_context="com.tutorials.hp.primitivespassing.MainActivity">

        <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_main" />

        <android.support.design.widget.FloatingActionButton
            android_id="@+id/fab"
            android_layout_width="wrap_content"
            android_layout_height="wrap_content"
            android_layout_gravity="bottom|end"
            android_layout_margin="@dimen/fab_margin"
            android_src="@android:drawable/ic_dialog_email" />

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

2. content_main.xml

  • Let’s add our edittexts and checkbox here.

3. activity_second.xml

  • Here’s the code for the second activity.
  • This activity will receive data from the main activity and show it here.

JAVA CLASSES

We have two classes or two activities:

  1. MainActivity.java
  2. SecondActivity.java

1. MainActivity class

  • Our MainActivity.
  • We’ll pass data from this activity ti the second activity.

2. Second Activity class

  • Our second activity.
  • Will receive data from main activity and show them in textviews and checkbox.

Leave a Reply

Your email address will not be published. Required fields are marked *

X