Pass User Inputted Data using Android Intent

Pass User Inputted Data using Android Intent

Previously we have seen how you can create an Activity and make use of it in Android Application and also about the intent. This article tells you about how you can pass user inputted data using android intent from one activity to another activity.

APP CODE:

So start with creating new android application by giving proper name and all other parameters. Add another new activity where we will send our data from our default activity so it can process further on that data. When you create new activity make sure you create both XML and JAVA files for that activity. Inside JAVA file, you have to declare onCreate() method that will use setContentView() method to reference XML file to display to the user. Finally, you have to register new activity to the AndroidManifest.xml file same as our default activity.

second_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/Second"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvFinal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Now on main activity layout file add an button that we will use to move data to our newly created activity. For that we have to specify an button click method in our activity’s java file. Inside that method we will start another activity by passing an intent to startActivity() method. The intent is the thing which will take data from one activity to other activity. In between, declaring intent object and starting activity we bind our data with intent so it can travel to our new activity. So for that we have to use intent.putExtra(key, value) method that will bind data in Key+Value mechanism.  So we will take some data from user & send that to our new activity.

MainActivity.java

package com.purvik.datapassing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
    
    EditText edtText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		edtText = (EditText)findViewById(R.id.edtText);
		
	}
	
	public void goClick(View v) {
		
		Intent i = new Intent(MainActivity.this, SecondActivity.class);
		i.putExtra("DATA", edtText.getText().toString());
		startActivity(i);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.purvik.datapassing"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

So add an editText box to our main activity and give it a proper id so we can create an object of that to fetch inputted data. Now declare and create an object in MainActivity.java file, get the user input data by getText().toString() method. Next, add this data to our putExtra() method so it will stay bind with our intent when it get reach to new activity.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    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.purvik.datapassing.MainActivity" >

    <EditText
        android:id="@+id/edtText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edtText"
        android:layout_centerHorizontal="true"
        android:onClick="goClick"
        android:text="GO" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/first"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Inside our new activitie’s java file, declare one object of Intent ‘oldIntent’ and initialized it equal to getIntent() method. That will get the intent passed from our previous activity and store it in this local intent object. To retrieve binded data, use getStringExtra(key) method where you have to specify name of KEY as a parameter that we have used at the time of binding this data. Now  use Toast to display this fetch data so we can ensure that data we have passed one activity to other activity is correct or not. You can also take TextView on Second Activity and set it’s text as retrieved data using setText() method of TextView view.

SecondActivity.java

package com.purvik.datapassing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class SecondActivity extends Activity {
    Intent oldIntent;
    TextView tvFinal;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second_activity);
		tvFinal = (TextView)findViewById(R.id.tvFinal);
		
		oldIntent = getIntent();
		Toast.makeText(getApplicationContext(), "Data is:" + oldIntent.getStringExtra("DATA"), Toast.LENGTH_LONG).show();
		tvFinal.setText(oldIntent.getStringExtra("DATA"));
		
	}

}

App Output:
pass user inputted data using Android Intent

First Activity

Pass User Inputted Data Using Android Intent

Second Activity

So that it is what all about passing data between Android Activities with the help of intent. You can also get so many other benefits of intent as it holds lot of different Actions and Categories to work upon. You can learn about it from one of our previous articles. And ya, you can always comment your queries so we can resolve you doubts.

Leave a Reply

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