0
A few months ago, when I started working with Android, I built an Employee Directory application as an experimentation project. I thought this application could be useful to other developers starting with Android, so I’ve made it available as a Google Code project. The Employee Directory application is built in six easy steps, each building on top of each other. The end result is a simple, yet functional application that allows you to:
  • Look up employees by name in a local SQLite database.
  • Look at the details of an employee.
  • Call, email, text an employee from within the application.
  • Navigate up and down the organization’s org chart.
You can download the Eclipse projects here. android tutorial-1.0.zip includes a project for each step. Employee Directory 6 is the final application. If you prefer, you can also browse the source code here.

Setting Up and Running the Projects in Eclipse :-


Step 1: Basic Layout


 In this first step, we define the user interface for searching employees.

                                     

 Code highlights:
EmployeeList.java: The default Activity of the application. setContentView() is used to set the layout to main.xml.
main.xml: the layout for the default activity of the application.

Step 2: Working with Lists
In this second step, we add a List View component that will be used (in the following step) to display the list of employees matching the search criteria. In this step, we just use an Array Adapter to display sample data in the list.
 
                             

main.xml: The updated layout with the List View.
EmployeeList.java: An Array Adapter is used to populate the List View.

Step 3: Working with a SQLite Database

In this third step, we use a SQLite database to persist the employees. When the user clicks the Search button, we query the database, and populate the list with the employees matching the search criteria.

                                

 Code highlights:
  • In onCreate(), we use the DatabaseHelper class to open the database.
  • In search(), we use a Cursor to query the database. We then use a SimpleCursorAdapter to bind the ListView to the Cursor using a custom layout (employee_list_item) to display each item in the list.
DatabaseHelper.java: We extend SQLiteOpenHelper to manage the access to our database: If the database doesn’t yet exist, we create the employee table and populate it with sample data.
employee_list_item.xml: Layout to display each item in the list.

Step 4: Using Intents and passing information between Activities

In this fourth step, we create an Activity to display the details of the employee selected in the list. We start the EmployeeDetails activity by creating an Intent.

                               

Code highlights:
EmployeeList.java: in onListItemClick, we create a new Intent for the EmployeeDetails class, add the employee id to the intent using intent.putExtra(), and start a new Activity.
EmployeeDetails.java: The Employee details activity. We retrieve the id of the employee using getIntent().getIntExtra(). We then use a Cursor to retrieve the employee details.
employee_details.xml: A simple layout to display the details of an employee.

Step 5: Calling, Emailing, and Texting an Employee

In this fifth step, we interact with some of the built-in capabilities of our phone. We use Intents to allow the user to call, email, or text an employee. We reuse the EmployeeDetails Activity to allow the user to display the details of the manager of the selected employee.

  Code highlights:
  • In onCreate(), we build an array of actions (call, email, sms, view manager) available to the user depending on the information available for the displayed employee (for example, we only create a “Call mobile” action if the employee’s mobile phone number is available in the database).
  • EmployeeActionAdapter is a custom list adapter that binds each action in the actions array to the action_list_item layout.
  • In onListItemClick(), we create an Intent corresponding to the action selected by the user, and start a new activity with that intent.
action_list_item.xml: Layout for each action in the actions list.
employee_details.xml: Updated employee details layout.

Step 6: Navigating Up and Down the Org Chart

In this sixth step, we create a new Activity to display the Direct Reports of the selected employee, allowing the user of the application to navigate up and down the org chart of the organization. We also improve some elements of the application: for example, we polish the user interface in several layouts, and we populate the database using an XML documents as opposed to the hardcoded sample data used in the previous steps.
                             
                             

          Code highlights:
DirectReports.java: A new Activity to display the direct reports of a specific employee.
direct_reports.xml: The layout for the DirectReports Activity.

                                   

EmployeeDetails.java: “View direct reports” is added to the list of actions. When the user selects that action, a new Intent is created for the DirectReports Activity, and a new Activity is started using that Intent.
DatabaseHelper.java: Instead of populating the database with hardcoded sample data, the employee table is now created and populated from an XML file (sql.xml).
sql.xml: The xml file used to create and populate the employee table.

Post a Comment

 
Top