Difference between revisions of "Teams Winter 2011/team1/Android/Add Contact"

From CDOT Wiki
Jump to: navigation, search
(6.1. Create 'add' layout)
Line 1: Line 1:
== 6. Add Contact ==
+
== 7. Add Contact ==
=== 6.1. Create 'add' layout ===
+
=== 7.1. Create 'add' layout ===
6.1.1. Right click on ''layout -> New -> Android XML File'' .<br/>
+
7.1.1. Right click on ''layout -> New -> Android XML File'' .<br/>
6.1.2. Enter ''add.xml'' for the file name and check ''Layout'':<br/>
+
7.1.2. Enter ''add.xml'' for the file name and check ''Layout'':<br/>
6.1.3. Using ''Linear Layout'', create this layout for adding contact information:<br/>
+
7.1.3. Using ''Linear Layout'', create this layout for adding contact information:<br/>
 
[[Image: add_layout.png | 200px]]<br/>
 
[[Image: add_layout.png | 200px]]<br/>
6.1.4. This is the xml implementation:
+
7.1.4. This is the xml implementation:
 
<source lang="java">
 
<source lang="java">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
Line 33: Line 33:
 
</source>
 
</source>
  
=== 6.2. Create Add Contact Activity ===
+
=== 7.2. Create Add Contact Activity ===
 
This class will be responsible for taking care of adding new contact to the contact list.
 
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.
+
7.2.1. Create a new class called ''AddContactActivity.java'' in the same package as the main activity file.
 
<source lang="java">
 
<source lang="java">
 
package cs.ecl.team1.android;
 
package cs.ecl.team1.android;
Line 44: Line 44:
 
</source>
 
</source>
  
6.2.2. Add some fields to save the added information to 'Add Contact' by user.
+
7.2.2. Add some fields to save the added information to 'Add Contact' by user.
 
<source lang="java">
 
<source lang="java">
 
     private String firstName;
 
     private String firstName;
Line 51: Line 51:
 
</source>
 
</source>
  
6.2.3. On clicking the ''Save Contact'' button, the info from ''EditText''s will be assigned to the corresponding fields.
+
7.2.3. On clicking the ''Save Contact'' button, the info from ''EditText''s will be assigned to the corresponding fields.
 
<source lang="java">
 
<source lang="java">
 
     firstName = ((EditText) findViewById(R.id.firstName)).getText().toString();
 
     firstName = ((EditText) findViewById(R.id.firstName)).getText().toString();
Line 58: Line 58:
 
</source>
 
</source>
  
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.
+
7.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.
 
<source lang="java">
 
<source lang="java">
 
     @Override
 
     @Override
Line 76: Line 76:
 
</source>
 
</source>
  
=== 6.3. Add Contact Activity to the Android Manifest ===
+
=== 7.3. Add Contact Activity to the Android Manifest ===
6.3.1. Just add this line to the list of activities in ''AndroidManifest.xml''
+
7.3.1. Just add this line to the list of activities in ''AndroidManifest.xml''
 
<source lang="java">
 
<source lang="java">
 
   <activity android:label="Add Contact" android:name="AddContactActivity"></activity>
 
   <activity android:label="Add Contact" android:name="AddContactActivity"></activity>
 
</source>
 
</source>
  
=== 6.4. Implement Add Contact in the main activity class ===
+
=== 7.4. Implement Add Contact in the main activity class ===
6.4.1. First step is to create an instance of ''Intent'' to pass the current object to the activity and then start the activity for the ''Add Contact'' when it's selected from the menu items.
+
7.4.1. First step is to create an instance of ''Intent'' to pass the current object to the activity and then start the activity for the ''Add Contact'' when it's selected from the menu items.
 
<source lang="java">
 
<source lang="java">
 
     case R.id.item1:
 
     case R.id.item1:
Line 91: Line 91:
 
</source>
 
</source>
  
6.4.2. In ''startActivityForResult()'', two things need to be checked first, ''resultCode'' and the name of the activity, if these two were fine, the new student with the added values will be added to the list of students, otherwise there will be an alert message displayed to the user.
+
7.4.2. In ''startActivityForResult()'', two things need to be checked first, ''resultCode'' and the name of the activity, if these two were fine, the new student with the added values will be added to the list of students, otherwise there will be an alert message displayed to the user.
 
<source lang="java">
 
<source lang="java">
 
     @Override
 
     @Override
Line 109: Line 109:
  
  
== 6.5. Add Contact in Action ==
+
== 7.5. Add Contact in Action ==
6.5.1. Select the 'Add Contact' in the menu<br/>
+
7.5.1. Select the 'Add Contact' in the menu<br/>
 
[[Image: menu.png | 200px]]<br/><br/>
 
[[Image: menu.png | 200px]]<br/><br/>
 
[[Image: add_1.png | 200px]]<br/>
 
[[Image: add_1.png | 200px]]<br/>
  
6.5.2. Enter information of the new contact<br/>
+
7.5.2. Enter information of the new contact<br/>
 
[[Image: add_2.png | 200px]]<br/>
 
[[Image: add_2.png | 200px]]<br/>
  
6.5.3. The new contact will be appeared in the list of contacts.<br/>
+
7.5.3. The new contact will be appeared in the list of contacts.<br/>
 
[[Image: added.png | 200px]]<br/>
 
[[Image: added.png | 200px]]<br/>

Revision as of 12:14, 28 March 2011

7. Add Contact

7.1. Create 'add' layout

7.1.1. Right click on layout -> New -> Android XML File .
7.1.2. Enter add.xml for the file name and check Layout:
7.1.3. Using Linear Layout, create this layout for adding contact information:
Add layout.png
7.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>

7.2. Create Add Contact Activity

This class will be responsible for taking care of adding new contact to the contact list. 7.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 {

7.2.2. Add some fields to save the added information to 'Add Contact' by user.

    private String firstName;
    private String lastName;
    private String email;

7.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();

7.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();
    }

7.3. Add Contact Activity to the Android Manifest

7.3.1. Just add this line to the list of activities in AndroidManifest.xml

   <activity android:label="Add Contact" android:name="AddContactActivity"></activity>

7.4. Implement Add Contact in the main activity class

7.4.1. First step is to create an instance of Intent to pass the current object to the activity and then start the activity for the Add Contact when it's selected from the menu items.

    case R.id.item1:
       Intent i = new Intent(this, AddContactActivity.class);
       startActivityForResult(i, CREATE_CONTACT);    // to process the result that is returned from the AddContactActivity
       break;

7.4.2. In startActivityForResult(), two things need to be checked first, resultCode and the name of the activity, if these two were fine, the new student with the added values will be added to the list of students, otherwise there will be an alert message displayed to the user.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode == Activity.RESULT_OK && requestCode == CREATE_CONTACT) {
            studentList.addElement(new Student(intent.getExtras().getString("firstName"),
                    intent.getExtras().getString("lastName"), intent.getExtras().getString("email")));
        }
        else {
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setMessage("No contact was added!");
            alertDialog.show();
        }
        displayData(studentList);
    }


7.5. Add Contact in Action

7.5.1. Select the 'Add Contact' in the menu
Menu.png

Add 1.png

7.5.2. Enter information of the new contact
Add 2.png

7.5.3. The new contact will be appeared in the list of contacts.
Added.png