How to pass values between Activities Android ?
How to pass values between Activities Android ?
How to pass values between Activities Android ? is the familiar question from all the android developers. As a android developer apart from learning navigation between screens, important to understand how to pass values between activities is also mandatory thing to know to develop any android apps.
In this program we are passing the values from the screen 1 to screen 2, which is technically a passing the values between the activities.
Before learning How to pass values between Activities Android ? its recommended to learn navigate between android activities,
Table of Contents
MainActivity.java:
[java]
package in.javadomain.screennavigationpassvalue;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
Button screen1Button;
EditText txtValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen1Button = (Button) findViewById(R.id.btnMainPage);
txtValue = (EditText) findViewById(R.id.editText);
screen1Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("in.javadomain.screennavigationpassvalue.MainActivity1");
intent.putExtra("screen1_value", txtValue.getText().toString());
startActivity(intent);
}
});
}
}
[/java]
MainActivity1.java:
[java]
package in.javadomain.screennavigationpassvalue;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.EditText;
/**
* Created by Naveen on 12/30/2014.
*/
public class MainActivity1 extends ActionBarActivity{
EditText screen2EditTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
Intent intent = getIntent();
String message = intent.getStringExtra("screen1_value");
screen2EditTxt = (EditText) findViewById(R.id.editText2);
screen2EditTxt.setText(message);
}
}
[/java]
activity_main.xml:
[xml]
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Android Value Passing from one Screen to Another screen"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_marginTop="59dp"
android:layout_alignLeft="@+id/btnMainPage"
android:layout_alignStart="@+id/btnMainPage"
android:layout_alignRight="@+id/btnMainPage"
android:layout_alignEnd="@+id/btnMainPage" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_marginTop="59dp"
android:width="250dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to Pass Value to Next Screen"
android:id="@+id/btnMainPage"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="www.Javadomain.in"
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
[/xml]
activity_main1.xml:
[xml]
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="www.Javadomain.in"
android:id="@+id/textView3"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Android Value Passing from one Screen to Another screen"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_marginTop="59dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:width="150dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
[/xml]
AndroidManifest.xml:
[xml]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.javadomain.screennavigationpassvalue" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="in.javadomain.screennavigationpassvalue.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="in.javadomain.screennavigationpassvalue.MainActivity1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="in.javadomain.screennavigationpassvalue.MainActivity1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
[/xml]
Output Screenshots:
Share your comments/feedbacks if any to improve our blogging!