Intent Basics Passing One Activity To Another Activity

R.layout.single_item

Intent which is used for data transferring from one activity to another activity . Each Activity register in menifest xml

Intent which is used for data transferring from one activity to another activity . Each Activity register in menifest xml

Intent which is used for data transferring from one activity to another activity . Each Activity register in menifest xml

Intent which is used for data transferring from one activity to another activity . Each Activity register in menifest xml

FirstActivity[FirstActivity.java]

package com.androidfeeders.activitydatapassing;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class FirstActivity extends AppCompatActivity implements View.OnClickListener {

    Button buttonPassData;
    EditText editText;
    String text;

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

        buttonPassData= (Button)findViewById(R.id.button);
        editText= (EditText) findViewById(R.id.edit_text);
        buttonPassData.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)

    {

        int id =view.getId();
        text=editText.getText().toString();
        switch (id)
        {
            case R.id.button:

                if( !text.isEmpty())
                {
                    Intent intent= new Intent(this,SecondActivity.class);
                    intent.putExtra("keytext",text);
                    startActivity(intent);
                }

                else
                {
                 showToastMessage("Please Enter Text");
                }

                break;
        }


    }


    public void showToastMessage(String message) {
        Toast.makeText(FirstActivity.this, message, Toast.LENGTH_LONG).show();
    }
}

SecondActivity[SecondActivity.java]

package com.androidfeeders.activitydatapassing;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {


    TextView textView;


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


        //Previous activity edittext data
        Bundle bundle = getIntent().getExtras();
        String message = bundle.getString("keytext");


        textView=(TextView)findViewById(R.id.textView);
        textView.setText(message);

    }
}

Layout Resource[first_activity.xml]

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/constraintLayout"
    tools:context=".FirstActivity">


    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Type Something ..."
        android:textAllCaps="false"
        android:padding="5sp"
        android:layout_margin="20dp"
        app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"/>


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me! Send Activity Data"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:padding="20dp"
        android:layout_margin="60dp"
        android:background="@color/colorAccent"
        app:layout_constraintLeft_toLeftOf="@+id/edit_text"
        app:layout_constraintTop_toBottomOf="@+id/edit_text"
        android:gravity="center"
        />

</android.support.constraint.ConstraintLayout>

Layout Resource[second_activity.xml]

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:gravity="center"
        android:padding="40dp"
        android:layout_height="wrap_content"
        android:text="TextView"
         />


</android.support.constraint.ConstraintLayout>

Gradle Scripts~build.gradle[Module:app]

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.androidfeeders.activitydatapassing"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Gradle Scripts~build.gradle[Project:ActivityDataPassing]

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


Resource[strings.xml]

<resources>
    <string name="app_name">ActivityDataPassing</string>
</resources>

manifests

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidfeeders.activitydatapassing">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".SecondActivity" android:label="Second Activity"/>
    </application>

</manifest>

Ramasamy

I started to learn android and Kotlin and iOS and Hybrid application and i cant code IOS very well. But i love more interesting to code and finding new developer minds who are join to me You're welcome to keep on using my website.Instead, you can join our FB page for developer comments and post there: https://www.facebook.com/ramasamy.m.779 Best wishes :) Ramasamy(Software Developer)

Android Development and IOS Development & React Native Application

See what's new in Android & IOS & Hybrid development … explore and learn in site

About Ramasamy

Kotlin and React native example too

Related Posts

3 Comments

Ramasamy

5 min ago

Kotlin and React native example too

Reply

Ramasamy

5 min ago

Kotlin and React native example too

Reply

Ramasamy

5 min ago

Kotlin and React native example too

Reply

Leave a reply