Introduction

- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#FFC6A9"
- >
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Enter Name: "/>
- <EditText android:id="@+id/editText1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="10dip"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Enter Age: "
- />
- <EditText android:id="@+id/editText2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="10dip"
- android:inputType="number"/>
- <Button android:id="@+id/sendButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Send"
- android:layout_marginTop="15dip"/>
- </LinearLayout>
Step 3
Create another XML file with the following contents:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#FFC6A9">
- <TextView android:id="@+id/textViewName"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="15dip"
- android:textSize="18dip"/>
- <TextView android:id="@+id/txtViewAge"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="15dip"
- android:textSize="18dip"/>
- <Button android:id="@+id/backButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="15dip"
- android:text="Back"/>
- </LinearLayout>
Step 4
Create a Java class file with the following contents:
- package com.jsonparsingexample;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends Activity {
- // Initializing variables
- EditText Name;
- EditText Age;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Name = (EditText) findViewById(R.id.editText1);
- Age = (EditText) findViewById(R.id.editText2);
- Button btnNextScreen = (Button) findViewById(R.id.sendButton);
- //Listening to button event
- btnNextScreen.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- //Starting a new Intent
- Intent nextScreen = new Intent(getApplicationContext(), SecondActivity.class);
- //Sending data to another Activity
- nextScreen.putExtra("Name", Name.getText().toString());
- nextScreen.putExtra("Age", Age.getText().toString());
- startActivity(nextScreen);
- }
- });
- }
- }
Step 5
Create another Java class file with the following contents:
- package com.jsonparsingexample;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class SecondActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.second);
- TextView txtName = (TextView) findViewById(R.id.textViewName);
- TextView txtEmail = (TextView) findViewById(R.id.txtViewAge);
- Button btnBack = (Button) findViewById(R.id.backButton);
- Intent i = getIntent();
- // Receiving the Data
- String name = i.getStringExtra("Name");
- String age = i.getStringExtra("Age");
- Log.e("Second Screen", name + "." + age);
- // Displaying Received data
- txtName.setText(name);
- txtEmail.setText(age);
- // Binding Click event to Button
- btnBack.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- //Closing SecondScreen Activity
- finish();
- }
- });
- }
- }
Step 6
Add a Second Activity name in the Android manifest.xml file with the following contents:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.jsonparsingexample"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="7"
- android:targetSdkVersion="16" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.jsonparsingexample.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>
- <uses-permission android:name="android.permission.INTERNET"/>
- </manifest>
Step 7
Step 8


Step 9

Join the conversation! Your thoughts help the community grow.