Building an Android Application in 6 Steps

  • 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. includes a project for each step. EmployeeDirectory6 is the final application. If you prefer, you can also browse the source codehere.

Setting Up and Running the Projects in Eclipse

Here are some quick steps to set up and run the projects in Eclipse. If you just want to see how the application is built without setting up the projects, go straight to Step 1 below.

  1. Download here (or check out the project from the trunk).
  2. Follow the steps described here to install the Android SDK and the ADT plugin for Eclipse.
  3. In Eclipse, create a new Workspace and set its default Java Compiler compliance level to 1.5.
  4. Import the projects: File > Import > General > Existing Projects into Workspace, and point to the androidtutorial directory.
  5. Click here for instructions on how to run your projects using the Android emulator or on your device.

Step 1: Basic Layout

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

Code highlights: 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 ListView 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 ArrayAdapter to display sample data in the list.

Code highlights:

main.xml: The updated layout with the ListView. An ArrayAdapter is used to populate the ListView.

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. 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: 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. 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: A new Activity to display the direct reports of a specific employee.

direct_reports.xml: The layout for the DirectReports Activity. “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. 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.

By beatsoftech

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s