0% found this document useful (0 votes)
43 views11 pages

Intent 2

This document describes creating an Android application with two activities - FormActivity and DisplayActivity. FormActivity contains a form that users can fill out and submit. When submitted, it passes the form data to DisplayActivity using an intent bundle. DisplayActivity then displays the received form data. The application uses XML layout files form.xml and display.xml to define the user interfaces for each activity.

Uploaded by

Jb Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views11 pages

Intent 2

This document describes creating an Android application with two activities - FormActivity and DisplayActivity. FormActivity contains a form that users can fill out and submit. When submitted, it passes the form data to DisplayActivity using an intent bundle. DisplayActivity then displays the received form data. The application uses XML layout files form.xml and display.xml to define the user interfaces for each activity.

Uploaded by

Jb Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Project Description

In this Android tutorial, we will create an example to send data from one activity to another.
This example creates two activities (FormActivity and DisplayActivity) each doing the
following functions,
o FormActivity this is the main activity which is presented to the user when
launching the application for the first time. It displays a form and allows the user to
fill and submit the form. When submitted, it retrieves the values filled by the user
and stores as Bundle in Intent and starts another activity DisplayActivity.
Uses form.xml as layout file to display the form.
o DisplayActivity it retrieves the Bundle from Intent which has values filled by the
user and display it by setting the value of view elements. Uses display.xml as
layout file to display the result.
Environment Used
JDK 6 (Java SE 6)
Eclipse Indigo IDE for Java EE Developers (3.7.1)
Android SDK 4.0.3 / 4.1 Jelly Bean
Android Development Tools (ADT) Plugin for Eclipse (ADT version 20.0.0)
Setting up development environment
To run this example, you need to setup the development environment with at least one
Android platform in your SDK environment. To install Android SDK and configure ADT plugin
for Eclipse read this page .
Prerequisites
Before proceeding with this example, it is recommended that you have completed
this HelloWorld example which explains about creating Android project, its structure,
components and how to run it in Eclipse IDE.
Android Project
Create an Android project and name it as ActivityDemo.
strings.xml
Open res/values/string.xml and replace it with following content.
01
02 <?xml version="1.0" encoding="utf-8"?>
03 <resources>
<string name="hello">Form</string>
04 <string name="display">Display</string>
05 <string name="app_name">ActivityDemo</string>
06 <string name="name">Name</string>
07 <string name="age">Age</string>
08 <string name="female">Female</string>
<string name="male">Male</string>
09 <string name="gender">Gender</string>
10 <string name="submit">Submit</string>
11 </resources>
12
XML layout files
This application uses two layout files,
res/layout/form.xml Represents first screen used by FormActivity.java

res/layout/display.xml Represents second screen used by DisplayActivity.java


Create a new xml file in res/layout and name it as form.xml and copy the following content.
form.xml
01 <?xml version="1.0" encoding="utf-8"?>
02 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03 android:layout_width="fill_parent"
android:layout_height="fill_parent" >
04
05
<TextView
06 android:id="@+id/nameText"
07 android:layout_width="wrap_content"
08 android:layout_height="wrap_content"
09 android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
10 android:layout_marginLeft="18dp"
11 android:layout_marginTop="24dp"
12 android:text="@string/name" />
13
14 <EditText
15 android:id="@+id/name"
android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:layout_alignParentRight="true"
18 android:layout_alignTop="@+id/nameText"
19 android:ems="10" android:inputType="text"/>
20
<TextView
21 android:id="@+id/ageText"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:layout_alignLeft="@+id/nameText"
25 android:layout_below="@+id/name"
android:layout_marginTop="26dp"
26 android:text="@string/age" />
27
28 <EditText
29 android:id="@+id/age"
30 android:layout_width="wrap_content"
android:layout_height="wrap_content"
31
android:layout_alignParentRight="true"
32 android:layout_alignTop="@+id/ageText"
33 android:ems="10" android:inputType="text"/>
34
35 <TextView
36 android:id="@+id/genderText"
android:layout_width="wrap_content"
37 android:layout_height="wrap_content"
38 android:layout_alignLeft="@+id/gridLayout1"
39 android:layout_below="@+id/age"
40 android:layout_marginTop="19dp"
41 android:text="@string/gender" />
42
43 <GridLayout
44 android:id="@+id/gridLayout1"
android:layout_width="wrap_content"
45 android:layout_height="wrap_content"
46 android:layout_alignLeft="@+id/ageText"
47 android:layout_below="@+id/age"
48 android:layout_marginTop="44dp" >
49
<RadioGroup
50 android:id="@+id/gender"
51 android:layout_width="fill_parent"
52 android:layout_height="wrap_content"
53 android:orientation="horizontal" >
54
55 <RadioButton
android:id="@+id/femaleRadio"
56 android:layout_width="wrap_content"
57 android:layout_height="wrap_content"
58 android:checked="true"
59 android:text="@string/female" />
60
<RadioButton
61
android:id="@+id/maleRadio"
62 android:layout_width="wrap_content"
63 android:layout_height="wrap_content"
64 android:checked="true"
65 android:text="@string/male" />
</RadioGroup>
66 </GridLayout>
67
68 <Button
69 android:id="@+id/submit"
70 android:layout_width="wrap_content"
71 android:layout_height="wrap_content"
android:layout_alignRight="@+id/gridLayout1"
72 android:layout_below="@+id/gridLayout1"
73 android:layout_marginTop="21dp"
74 android:text="@string/submit" />
75 </RelativeLayout>
76
77
78
79
80
81
82
83
84
85
86
87
88
Create a new xml file in res/layout and name it as display.xml and copy the following
content.
display.xml
01 <?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
02 android:layout_width="fill_parent"
03 android:layout_height="fill_parent"
04 android:columnCount="3" >
05
06 <TextView
07 android:id="@+id/nameText"
android:layout_column="1"
08 android:layout_gravity="left"
09 android:layout_row="1"
10 android:text="@string/name" />
11
12 <TextView
android:id="@+id/nameValue"
13 android:layout_column="2"
14 android:layout_gravity="left"
15 android:layout_row="1"
16 android:text=""
17 android:textAppearance="?android:attr/textAppearanceMedium" />
18
<TextView
19 android:id="@+id/ageText"
20 android:layout_column="1"
21 android:layout_gravity="left"
22 android:layout_row="3"
23 android:text="@string/age" />
24
<TextView
25 android:id="@+id/ageValue"
26 android:layout_column="2"
27 android:layout_gravity="left"
28 android:layout_row="3"
android:text=""
29 android:textAppearance="?android:attr/textAppearanceMedium" />
30
31 <TextView
32 android:id="@+id/genderText"
33 android:layout_column="1"
34 android:layout_gravity="left"
android:layout_row="5"
35 android:text="@string/gender" />
36
37 <TextView
38 android:id="@+id/genderValue"
39 android:layout_column="2"
android:layout_gravity="left"
40 android:layout_row="5"
41 android:text=""
42 android:textAppearance="?android:attr/textAppearanceMedium" />
43
44 <Space
45 android:layout_width="21dp"
android:layout_height="1dp"
46 android:layout_column="0"
47 android:layout_gravity="fill_horizontal"
48 android:layout_row="0" />
49
50 <Space
android:layout_width="1dp"
51 android:layout_height="21dp"
52 android:layout_column="0"
53 android:layout_gravity="fill_horizontal"
54 android:layout_row="0" />
55
56 <Space
android:layout_width="64dp"
57 android:layout_height="1dp"
58 android:layout_column="1"
59 android:layout_gravity="fill_horizontal"
60 android:layout_row="0" />
61
<Space
62 android:layout_width="1dp"
63 android:layout_height="10dp"
64 android:layout_column="0"
65 android:layout_gravity="fill_horizontal"
66 android:layout_row="2" />
67
<Space
68 android:layout_width="1dp"
69 android:layout_height="10dp"
70 android:layout_column="0"
71 android:layout_gravity="fill_horizontal"
android:layout_row="4" />
72
</GridLayout>
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Activity
In src folder, create a new Class and name it as FormActivity in the package
com.theopentutorials.android and copy the following code.
FormActivity.java
package com.theopentutorials.android;
01
02 import android.app.Activity;
03 import android.content.Intent;
04 import android.os.Bundle;
05 import android.view.View;
import android.view.View.OnClickListener;
06
import android.widget.Button;
07 import android.widget.EditText;
08 import android.widget.RadioButton;
09 import android.widget.RadioGroup;
10
11 public class FormActivity extends Activity implements OnClickListener {
Button button;
12 RadioGroup genderRadioGroup;
13 EditText name;
14 EditText age;
15
16 /** Called when the activity is first created. */
17 @Override
public void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.form);
20
21 //Get the ids of view objects
22 findAllViewsId();
23
button.setOnClickListener(this);
24
}
25
26 private void findAllViewsId() {
27 button = (Button) findViewById(R.id.submit);
28 name = (EditText) findViewById(R.id.name);
29 age = (EditText) findViewById(R.id.age);
genderRadioGroup = (RadioGroup) findViewById(R.id.gender);
30 }
31
32 @Override
33 public void onClick(View v) {
34 Intent intent = new Intent(getApplicationContext(), DisplayActivity.class);
35 //Create a bundle object
Bundle b = new Bundle();
36
37 //Inserts a String value into the mapping of this Bundle
38 b.putString("name", name.getText().toString());
39 b.putString("age", age.getText().toString());
40 int id = genderRadioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
41
b.putString("gender", radioButton.getText().toString());
42
43 //Add the bundle to the intent.
44 intent.putExtras(b);
45
46 //start the DisplayActivity
startActivity(intent);
47 }
48 }
49
50
51
52
53
54
55
56
57
Here, the button is registered to the android.view.View.OnClickListener using the method
button.setOnClickListener(). When the event occurs it calls onClick() method.
We create an Intent object to activate another activity (DisplayActivity). Intent object is
created using one of its constructors which takes a Context of the application package
implementing this class and the component class that is to be used for the intent.

public Intent (Context packageContext, Class cls)


In our example, we use,
1 Intent intent = new Intent(getApplicationContext(), DisplayActivity.class);
To pass data between activities, we create a Bundle object and add values (name, age,
gender) and stuff this bundle object in the intent by calling the method putExtras().
In src folder, create another Class and name it as DisplayActivity in the same package and
copy the following code.
DisplayActivity.java
package com.theopentutorials.android;
01
02 import android.app.Activity;
03 import android.os.Bundle;
04 import android.widget.TextView;
05
06 public class DisplayActivity extends Activity {
07
08 @Override
protected void onCreate(Bundle savedInstanceState) {
09 super.onCreate(savedInstanceState);
10 setContentView(R.layout.display);
11
12 Bundle b = getIntent().getExtras();
13 TextView name = (TextView) findViewById(R.id.nameValue);
TextView age = (TextView) findViewById(R.id.ageValue);
14 TextView gender = (TextView) findViewById(R.id.genderValue);
15
16 name.setText(b.getCharSequence("name"));
17 age.setText(b.getCharSequence("age"));
18 gender.setText(b.getCharSequence("gender"));
}
19 }
20
21
22
23
The above code works as follows,
Retrieve bundle object from intent.
Find the id for view objects defined by display.xml
Get the value from bundle and set it in the view objects
AndroidManifest.xml
Define the two activities in AndroidManifest.xml file
01
02
03 <?xml version="1.0" encoding="utf-8"?>
04 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theopentutorials.android"
05 android:versionCode="1"
06 android:versionName="1.0" >
07
08 <uses-sdk android:minSdkVersion="15" />
09
10 <application
android:icon="@drawable/ic_launcher"
11
android:label="@string/app_name" >
12 <activity
13 android:name=".FormActivity"
14 android:label="@string/hello" >
15 <intent-filter>
<action android:name="android.intent.action.MAIN" />
16 <category android:name="android.intent.category.LAUNCHER" />
17 </intent-filter>
18 </activity>
19
20 <activity
21 android:name=".DisplayActivity"
android:label="@string/display" >
22 </activity>
23 </application>
24 </manifest>
25
26
Output
Run your application.
Project Folder Structure
The complete folder structure of this example is shown below.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy