Difference between revisions of "Teams Winter 2011/team1/Android/Add Contact"
(→6.2. Create Add Contact Activity) |
(→6.3. Add Contact Activity to the Android Manifest) |
||
Line 80: | Line 80: | ||
=== 6.3. Add Contact Activity to the Android Manifest === | === 6.3. Add Contact Activity to the Android Manifest === | ||
+ | 6.3.1. Just add this line to the list of activities in ''AndroidManifest.xml'' | ||
+ | <source lang="java"> | ||
+ | <activity android:label="Add Contact" android:name="AddContactActivity"></activity> | ||
+ | </source> | ||
+ | |||
=== 6.4. Implement Add Contact in the main activity class === | === 6.4. Implement Add Contact in the main activity class === |
Revision as of 16:20, 27 March 2011
Contents
6. Add Contact
6.1. Create 'add' layout
6.1.1. Right click on layout -> New -> Android XML File .
6.1.2. Enter add.xml for the file name and check Layout:
6.1.3. Using Linear Layout, create this layout for adding contact information:
6.1.4. This is the xml implementation:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/titleTextView"
android:gravity="center_vertical" android:textSize="30dp"
android:layout_gravity="center" android:text="@string/addContact_textView"></TextView>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_marginBottom="3dp" android:hint="@string/hint_first_name" android:id="@+id/firstName"></EditText>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:isScrollContainer="true"
android:layout_marginBottom="3dp" android:hint="@string/hint_last_name" android:id="@+id/lastName"></EditText>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_marginBottom="3dp" android:hint="@string/hint_email" android:id="@+id/email"></EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/saveContact_button"
android:text="@string/saveButton" android:gravity="center_horizontal"
android:layout_gravity="center_horizontal" android:textSize="20dp"></Button>
</LinearLayout>
6.2. Create Add Contact Activity
This class will be responsible for taking care of adding new contact to the contact list. 6.2.1. Create a new class called AddContactActivity.java in the same package as the main activity file.
package cs.ecl.team1.android;
import android.app.Activity;
public class AddContactActivity extends Activity {
6.2.2. Add some fields to save the added information to 'Add Contact' by user.
private String firstName;
private String lastName;
private String email;
6.2.3. On clicking the Save Contact button, the info from EditTexts will be assigned to the corresponding fields.
firstName = ((EditText) findViewById(R.id.firstName)).getText().toString();
lastName = ((EditText) findViewById(R.id.lastName)).getText().toString();
email = ((EditText) findViewById(R.id.email)).getText().toString();
6.2.3. The activity will be finished by calling finish() function from which the super will be called. In finish(), a new Intent is created to be able to pass data back to the calling activity.
@Override
public void finish() {
Intent i = new Intent();
if (firstName.equals("") && lastName.equals("") && email.equals("")) {
setResult(0);
}
else {
i.putExtra("firstName", firstName);
i.putExtra("lastName", lastName);
i.putExtra("email", email);
setResult(RESULT_OK, i);
}
super.finish();
}
6.3. Add Contact Activity to the Android Manifest
6.3.1. Just add this line to the list of activities in AndroidManifest.xml
<activity android:label="Add Contact" android:name="AddContactActivity"></activity>