Post Ad Area

Monday, April 4, 2011

Get Started Developing For Android With Eclipse, Reloaded

In the first part of this tutorial series, we built a simple brew timer application using Android and Eclipse. In this second part, we’ll continue developing the application by adding extra functionality. In doing this, you’ll be introduced to some important and powerful features of the Android SDK, including Persistent data storage, Activities and Intent as well as Shared user preferences.
To follow this tutorial, you’ll need the code from the previous article. If you want to get started right away, grab the code from GitHub and check out the tutorial_part_1 tag using this:
1 Starting Point in Get Started Developing For Android With Eclipse, Reloaded
1$ git clone git://
2$ cd BrewClock
3$ git checkout tutorial_part_1
Once you’ve checked out the code on GitHub, you’ll need to import the project into Eclipse:
  1. Launch Eclipse and choose File → Import…
  2. In the Import window, select “Existing Projects into Workspace” and click “Next.”
  3. On the next screen, click “Browse,” and select the project folder that you cloned from GitHub.
  4. Click “Finish” to import your project into Eclipse.
After importing the project into Eclipse, you might receive a warning message:
1Android required .class compatibility set to 5.0.
2Please fix project properties.
If this is the case, right-click on the newly imported “BrewClock” project in the “Project Explorer,” choose “Fix Project Properties,” and then restart Eclipse.

Getting Started With Data Storage

Currently, BrewClock lets users set a specific time for brewing their favorite cups of tea. This is great, but what if they regularly drink a variety of teas, each with their own different brewing times? At the moment, users have to remember brewing times for all their favorite teas! This doesn’t make for a great user experience. So, in this tutorial we’ll develop functionality to let users store brewing times for their favorite teas and then choose from that list of teas when they make a brew.
To do this, we’ll take advantage of Android’s rich data-storage API. Android offers several ways to store data, two of which we’ll cover in this article. The first, more powerful option, uses the SQLite database engine to store data for our application.
SQLite is a popular and lightweight SQL database engine that saves data in a single file. It is often used in desktop and embedded applications, where running a client-server SQL engine (such as MySQL or PostgreSQL) isn’t feasible.
Every application installed on an Android device can save and use any number of SQLite database files (subject to storage capacity), which the system will manage automatically. An application’s databases are private and so cannot be accessed by any other applications. (Data can be shared through the ContentProvider class, but we won’t cover content providers in this tutorial.) Database files persist when the application is upgraded and are deleted when the application is uninstalled.
We’ll use a simple SQLite database in BrewClock to maintain a list of teas and their appropriate brewing times. Here’s an overview of how our database schema will look:
2| Table: teas                         |
4| Column     | Description            |
6| _ID        | integer, autoincrement |
7| name       | text, not null         |
8| brew_time  | integer, not null      |
If you’ve worked with SQL before, this should look fairly familiar. The database table has three columns: a unique identifier (_ID), name and brewing time. We’ll use the APIs provided by Android to create the database table in our code. The system will take care of creating the database file in the right location for our application.

Abstracting the Database

To ensure the database code is easy to maintain, we’ll abstract all the code for handling database creation, inserts and queries into a separate class, TeaData. This should be fairly familiar if you’re used to the model-view-controller approach. All the database code is kept in a separate class from our BrewClockActivity. The Activity can then just instantiate a new TeaData instance (which will connect to the database) and do what it needs to do. Working in this way enables us to easily change the database in one place without having to change anything in any other parts of our application that deal with the database.
Create a new class called TeaData in the BrewClock project by going to File → New → Class. Ensure that TeaData extends the android.database.sqlite.SQLiteOpenHelper class and that you check the box for “Constructors from superclass.”
2 Create Teadata Class1 in Get Started Developing For Android With Eclipse, Reloaded
The TeaData class will automatically handle the creation and versioning of a SQLite database for your application. We’ll also add methods to give other parts of our code an interface to the database.
Add two constants to TeaData to store the name and version of the database, the table’s name and the names of columns in that table. We’ll use the Android-provided constant BaseColumns._ID for the table’s unique id column:
01// src/com/example/brewclock/
03import android.content.ContentValues;
04import android.content.Context;
05import android.database.Cursor;
06import android.database.DatabaseUtils;
07import android.provider.BaseColumns;
09public class TeaData extends SQLiteOpenHelper {
10  private static final String DATABASE_NAME = "teas.db";
11  private static final int DATABASE_VERSION = 1;
13  public static final String TABLE_NAME = "teas";
15  public static final String _ID = BaseColumns._ID;
16  public static final String NAME = "name";
17  public static final String BREW_TIME = "brew_time";
19  // …
Add a constructor to TeaData that calls its parent method, supplying our database name and version. Android will automatically handle opening the database (and creating it if it does not exist).
1// src/com/example/brewclock/
2public TeaData(Context context) {
3  super(context, DATABASE_NAME, null, DATABASE_VERSION);
We’ll need to override the onCreate method to execute a string of SQL commands that create the database table for our tea. Android will handle this method for us, calling onCreate when the database file is first created.
On subsequent launches, Android checks the version of the database against the DATABASE_VERSION number we supplied to the constructor. If the version has changed, Android will call the onUpgrade method, which is where you would write any code to modify the database structure. In this tutorial, we’ll just ask Android to drop and recreate the database.
So, add the following code to onCreate:
01// src/com/example/brewclock/
03public void onCreate(SQLiteDatabase db) {
05  String sql =
06    "CREATE TABLE " + TABLE_NAME + " ("
08      + NAME + " TEXT NOT NULL, "
09      + BREW_TIME + " INTEGER"
10      + ");";
12  db.execSQL(sql);
16public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
18  onCreate(db);
Next, we’ll add a new method to TeaData that lets us easily add new tea records to the database. We’ll supply the method with a name and brewing time for the tea to be added. Rather than forcing us to write out the raw SQL to do this, Android supplies a set of classes for inserting records into the database. First, we create a set of ContentValues, pushing the relevant values into that set.
With an instance of ContentValues, we simply supply the column name and the value to insert. Android takes care of creating and running the appropriate SQL. Using Android’s database classes ensures that the writes are safe, and if the data storage mechanism changes in a future Android release, our code will still work.
Add a new method, insert(), to the TeaData class:
01// src/com/example/brewclock/
02public void insert(String name, int brewTime) {
03  SQLiteDatabase db = getWritableDatabase();
05  ContentValues values = new ContentValues();
06  values.put(NAME, name);
07  values.put(BREW_TIME, brewTime);
09  db.insertOrThrow(TABLE_NAME, null, values);

Retrieving Data

With the ability to save data into the database, we’ll also need a way to get it back out. Android provides the Cursor interface for doing just this. A Cursor represents the results of running a SQL query against the database, and it maintains a pointer to one row within that result set. This pointer can be moved forwards and backwards through the results, returning the values from each column. It can help to visualize this:
1SQL Query: SELECT * from teas LIMIT 3;
4|  _ID  |  name       |  brew_time  |
6|    1  |  Earl Grey  |          3  |
7|    2  |  Green      |          1  | <= Cursor
8|    3  |  Assam      |          5  |
In this example, the Cursor is pointing at the second row in the result set (Green tea). We could move the Cursor back a row to represent the first row (Earl Grey) by calling cursor.moveToPrevious(), or move forward to the Assam row with moveToNext(). To fetch the name of the tea that the Cursor is pointing out, we would call cursor.getString(1), where 1 is the column index of the column we wish to retrieve (note that the index is zero-based, so column 0 is the first column, 1 the second column and so on).
Now that you know about Cursors, add a method that creates a Cursor object that returns all the teas in our database. Add an all method to TeaData:
01// src/com/example/brewclock/
02public Cursor all(Activity activity) {
03  String[] from = { _ID, NAME, BREW_TIME };
04  String order = NAME;
06  SQLiteDatabase db = getReadableDatabase();
07  Cursor cursor = db.query(TABLE_NAME, from, null, null, null, null, order);
08  activity.startManagingCursor(cursor);
10  return cursor;
Let’s go over this method in detail, because it looks a little strange at first. Again, rather than writing raw SQL to query the database, we make use of Android’s database interface methods.
First, we need to tell Android which columns from our database we’re interested in. To do this, we create an array of strings—each one of the column identifiers that we defined at the top of TeaData. We’ll also set the column that we want to order the results by and store it in the order string.
Next, we create a read-only connection to the database using getReadableDatabase(), and with that connection, we tell Android to run a query using the query() method. The query() method takes a set of parameters that Android internally converts into a SQL query. Again, Android’s abstraction layer ensures that our application code will likely continue to work, even if the underlying data storage changes in a future version of Android.
Because we just want to return every tea in the database, we don’t apply any joins, filters or groups (i.e. WHERE, JOIN, and GROUP BY clauses in SQL) to the method. The from and order variables tell the query what columns to return on the database and the order in which they are retrieved. We use the SQLiteDatabase.query() method as an interface to the database.
Last, we ask the supplied Activity (in this case, our BrewClockActivity) to manage the Cursor. Usually, a Cursor must be manually refreshed to reload any new data, so if we added a new tea to our database, we would have to remember to refresh our Cursor. Instead, Android can take care of this for us, recreating the results whenever the Activity is suspended and resumed, by calling startManagingCursor().
Finally, we’ll add another utility method to return the number of records in the table. Once again, Android provides a handy utility to do this for us in the DatabaseUtils class:
Add the following method, count, to your TeaData class:
1// src/com/example/brewclock/
2  public long count() {
3    SQLiteDatabase db = getReadableDatabase();
4    return DatabaseUtils.queryNumEntries(db, TABLE_NAME);
5  }
Save the TeaData class, and fix any missing imports using Eclipse (Source → Organize Imports). With our data class finished, it’s time to change BrewClock’s interface to make use of the database!

Modify BrewClock’s Interface to Allow Tea Selection

The purpose of storing preset teas and brew times is to let the user quickly select their favorite tea from the presets. To facilitate this, we’ll add a Spinner (analogous to a pop-up menu in desktop interfaces) to the main BrewClock interface, populated with the list of teas from TeaData.
As in the previous tutorial, use Eclipse’s layout editor to add the Spinner to BrewClock’s main interface layout XML file. Add the following code just below the LinearLayout for the brew count label (around line 24). Remember, you can switch to the “Code View” tab along the bottom of the window if Eclipse opens the visual layout editor.
05  android:orientation="vertical"
06  android:layout_width="fill_parent"
07  android:layout_height="wrap_content">
09  <Spinner
10    android:id="@+id/tea_spinner"
11    android:layout_width="fill_parent"
12    android:layout_height="wrap_content" />
In the BrewClockActivity class, add a member variable to reference the Spinner, and connect it to the interface using findViewById:
01// src/com/example/brewclock/
02protected Spinner teaSpinner;
03protected TeaData teaData;
05// …
07public void onCreate(Bundle savedInstanceState) {
08  // …
09  teaData = new TeaData(this);
10  teaSpinner = (Spinner) findViewById(;
Try running your application to make sure the new interface works correctly. You should see a blank pop-up menu (or Spinner) just below the brew count. If you tap the spinner, Android handles displaying a pop-up menu so that you can choose an option for the spinner. At the moment, the menu is empty, so we’ll remedy that by binding the Spinner to our tea database.
3 Blank Spinner in Get Started Developing For Android With Eclipse, Reloaded

Data Binding

When Android retrieves data from a database, it returns a Cursor object. The Cursor represents a set of results from the database and can be moved through the results to retrieve values. We can easily bind these results to a view (in this case, the Spinner) using a set of classes provided by Android called “Adapters.” Adapters do all the hard work of fetching database results from a Cursor and displaying them in the interface.
Remember that our TeaData.all() method already returns a Cursor populated with the contents of our teas table. Using that Cursor, all we need to do is create a SimpleCursorAdapter to bind its data to our teaSpinner, and Android will take care of populating the spinner’s options.
Connect the Cursor returned by teaData.all() to the Spinner by creating a SimpleCursorAdapter:
01// com/example/brewclock/
03public void onCreate(Bundle savedInstanceState) {
04  // …
05  Cursor cursor = teaData.all(this);
07  SimpleCursorAdapter teaCursorAdapter = new SimpleCursorAdapter(
08    this,
09    android.R.layout.simple_spinner_item,
10    cursor,
11    new String[] { TeaData.NAME },
12    new int[] { }
13  );
15  teaSpinner.setAdapter(teaCursorAdapter);
16  teaCursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Notice that we’ve made use of Android’s built-in android.R object. This provides some generic default resources for your application, such as simple views and layouts. In this case, we’ve used android.R.layout.simple_spinner_item, which is a simple text label layout.
If you run the application again, you’ll see that the spinner is still empty! Even though we’ve connected the spinner to our database, there are no records in the database to display.
Let’s give the user a choice of teas by adding some default records to the database in BrewClock’s constructor. To avoid duplicate entries, we’ll add only the default teas if the database is empty. We can make use of TeaData’s count() method to check if this is the case.
Add code to create a default set of teas if the database is empty. Add this line just above the code to fetch the teas from teaData:
01// com/example/brewclock/
02public void onCreate(Bundle savedInstanceState) {
03  // …
05  // Add some default tea data! (Adjust to your preference :)
06  if(teaData.count() == 0) {
07    teaData.insert("Earl Grey", 3);
08    teaData.insert("Assam", 3);
09    teaData.insert("Jasmine Green", 1);
10    teaData.insert("Darjeeling", 2);
11  }
13  // Code from the previous step:
14  Cursor cursor = teaData.all(this);
16  // …
Now run the application again. You’ll now see that your tea Spinner has the first tea selected. Tapping on the Spinner lets you select one of the teas from your database!
4 Populated Spinner in Get Started Developing For Android With Eclipse, Reloaded
Congratulations! You’ve successfully connected your interface to a data source. This is one of the most important aspects of any software application. As you’ve seen, Android makes this task fairly easy, but it is extremely powerful. Using cursors and adapters, you can take virtually any data source (from a simple array of strings to a complex relational database query) and bind it to any type of view: a spinner, a list view or even an iTunes-like cover-flow gallery!
Although now would be a good time for a brew, our work isn’t over yet. While you can choose different teas from the Spinner, making a selection doesn’t do anything. We need to find out which tea the user has selected and update the brew time accordingly.

Read Selected Tea, and Update Brew Time

To determine which tea the user has selected from our database, BrewClockActivity needs to listen for an event. Similar to the OnClickListener event that is triggered by button presses, we’ll implement the OnItemSelectedListener. Events in this listener are triggered when the user makes a selection from a view, such as our Spinner.
Enable the onItemSelectedListener in BrewClockActivity by adding it to the class declaration. Remember to implement the interface methods onItemSelected() and onNothingSelected():
01// src/com/example/brewclock/
02public class BrewClockActivity extends Activity implements OnClickListener, OnItemSelectedListener {
03  // …
04  public void onItemSelected(AdapterView spinner, View view, int position, long id) {
05    if(spinner == teaSpinner) {
06      // Update the brew time with the selected tea’s brewtime
07      Cursor cursor = (Cursor) spinner.getSelectedItem();
08      setBrewTime(cursor.getInt(2));
09    }
10  }
12  public void onNothingSelected(AdapterView adapterView) {
13    // Do nothing
14  }
Here we check whether the spinner that triggered the onItemSelected event was BrewClock’s teaSpinner. If so, we retrieve a Cursor object that represents the selected record. This is all handled for us by the SimpleCursorAdapter that connects teaData to the Spinner. Android knows which query populates the Spinner and which item the user has selected. It uses these to return the single row from the database, representing the user’s selected tea.
Cursor’s getInt() method takes the index of the column we want to retrieve. Remember that when we built our Cursor in teaData.all(), the columns we read were _ID, NAME and BREW_TIME. Assuming we chose Jasmine tea in teaSpinner, the Cursor returned by our selection would be pointing at that record in the database.
We then ask the Cursor to retrieve the value from column 2 (using getInt(2)), which in this query is our BREW_TIME column. This value is supplied to our existing setBrewTime() method, which updates the interface to show the selected tea’s brewing time.
Finally, we need to tell the teaSpinner that BrewClockActivity is listening for OnItemSelected events. Add the following line to BrewClockActivity’s onCreate method:
1// src/com/example/brewclock/
2public void onCreate() {
3  // …
4  teaSpinner.setOnItemSelectedListener(this);
That should do it! Run your application again, and try selecting different teas from the Spinner. Each time you select a tea, its brew time will be shown on the countdown clock. The rest of our code already handles counting down from the current brew time, so we now have a fully working brew timer, with a list of preset teas.
You can, of course, go back into the code and add more preset teas to the database to suit your tastes. But what if we released BrewClock to the market? Every time someone wanted to add a new tea to the database, we’d need to manually update the database, and republish the application; everyone would need to update, and everybody would have the same list of teas. That sounds pretty inflexible, and a lot of work for us!
5 Default Teas in Get Started Developing For Android With Eclipse, Reloaded
It would be much better if the user had some way to add their own teas and preferences to the database. We’ll tackle that next…

Introducing Activities

Each screen in your application and its associated code is an Activity. Every time you go from one screen to another, Android creates a new Activity. In reality, although an application may comprise any number of screens/activities, Android treats them as separate entities. Activities work together to form a cohesive experience because Android lets you easily pass data between them.
In this final section, you’ll add a new Activity (AddTeaActivity) to your application and register it with the Android system. You’ll then pass data from the original BrewClockActivity to this new Activity.
First, though, we need to give the user a way to switch to the new Activity. We’ll do this using an options menu.

Options Menus

Options menus are the pop-up menus that appear when the user hits the “Menu” key on their device. Android handles the creation and display of options menus automatically; you just need to tell it what options to display and what to do when an option is chosen by the user.
However, rather than hard-coding our labels into the menu itself, we’ll make use of Android string resources. String resources let you maintain all the human-readable strings and labels for your application in one file, calling them within your code. This means there’s only one place in your code where you need to change strings in the future.
In the project explorer, navigate to “res/values” and you will see that a strings.xml file already exists. This was created by Eclipse when we first created the project, and it is used to store any strings of text that we want to use throughout the application.
Open strings.xml by double clicking on it, and switch to the XML view by clicking the strings.xml tab along the bottom of the window.
Add the following line within the element:
2  <resources>
4    <string name="add_tea_label">Add Teastring>
5  resources>
Here you’ve defined a string, add_tea_label, and its associated text. We can use add_tea_label to reference the string throughout the application’s code. If the label needs to change for some reason in the future, you’ll need to change it only once in this file.
Next, let’s create a new file to define our options menu. Just like strings and layouts, menus are defined in an XML file, so we’ll start by creating a new XML file in Eclipse:
Create a new Android XML file in Eclipse by choosing File → New → Other, and then select “Android XML File.”
Select a resource type of “Menu,” and save the file as main.xml. Eclipse will automatically create a folder, res/menu, where your menu XML files will be stored.
7 New Menu Xml in Get Started Developing For Android With Eclipse, Reloaded
Open the res/menus/main.xml file, and switch to XML view by clicking the “main.xml” tab along the bottom of the window.
Add a new menu item, add_tea.
1xml version="1.0" encoding="utf-8"?>
3  <item android:id="@+id/add_tea" android:title="@string/add_tea_label" />
Notice the android:title attribute is set to @string/add_tea_label. This tells Android to look up add_tea_label in our strings.xml file and return the associated label. In this case, our menu item will have a label “Add Tea.”
Next, we’ll tell our Activity to display the options menu when the user hits the “Menu” key on their device.
Back in, override the onCreateOptionsMenu method to tell Android to load our menu when the user presses the “Menu” button:
1// src/com/example/brewclock/
3public boolean onCreateOptionsMenu(Menu menu) {
4  MenuInflater inflater = getMenuInflater();
5  inflater.inflate(, menu);
7  return true;
When the user presses the “Menu” button on their device, Android will now call onCreateOptionsMenu. In this method, we create a MenuInflater, which loads a menu resource from your application’s package. Just like the buttons and text fields that make up your application’s layout, the main.xml resource is available via the global R object, so we use that to supply the MenuInflater with our menu resource.
To test the menu, save and run the application in the Android emulator. While it’s running, press the “Menu” button, and you’ll see the options menu pop up with an “Add Tea” option.
8 Add Teas Options Menu in Get Started Developing For Android With Eclipse, Reloaded
If you tap the “Add Tea” option, Android automatically detects the tap and closes the menu. In the background, Android will notify the application that the option was tapped.

Handling Menu Taps

When the user taps the “Add Tea” menu option, we want to display a new Activity so that they can enter the details of the tea to be added. Start by creating that new Activity by selecting File → New → Class.
9 New Activity Settings in Get Started Developing For Android With Eclipse, Reloaded
Name the new class AddTeaActivity, and make sure it inherits from the class. It should also be in the com.example.brewclock package:
01// src/com/example/brewclock/
02package com.example.brewclock;
05import android.os.Bundle;
07public class AddTeaActivity extends Activity {
08  @Override
09  public void onCreate(Bundle savedInstanceState) {
10    super.onCreate(savedInstanceState);
11  }
This simple, blank Activity won’t do anything yet, but it gives us enough to finish our options menu.
Add the onOptionsItemSelected override method to BrewClockActivity. This is the method that Android calls when you tap on a MenuItem (notice it receives the tapped MenuItem in the item parameter):
01// src/com/example/brewclock/
03public boolean onOptionsItemSelected(MenuItem item) {
04  switch(item.getItemId()) {
05    case
06      Intent intent = new Intent(this, AddTeaActivity.class);
07      startActivity(intent);
08      return true;
10    default:
11      return super.onOptionsItemSelected(item);
12  }
With this code, we’ve told Android that when the “Add Tea” menu item is tapped, we want to start a new Activity; in this case, AddTeaActivity. However, rather than directly creating an instance of AddTeaActivity, notice that we’ve used an Intent. Intents are a powerful feature of the Android framework: they bind Activities together to make up an application and allow data to be passed between them.
Intents even let your application take advantage of any Activities within other applications that the user has installed. For example, when the user asks to display a picture from a gallery, Android automatically displays a dialogue to the user allowing them to pick the application that displays the image. Any applications that are registered to handle image display will be shown in the dialogue.
Intents are a powerful and complex topic, so it’s worth reading about them in detail in the official Android SDK documentation.
Let’s try running our application to test out the new “Add Tea” screen.
Run your project, tap the “Menu” button and then tap “Add Tea.”
Instead of seeing your “Add Tea” Activity as expected, you’ll be presented with a dialogue that is all too common for Android developers:
10 Crash in Get Started Developing For Android With Eclipse, Reloaded
Although we created the Intent and told it to start our AddTeaActivity Activity, the application crashed because we haven’t yet registered it within Android. The system doesn’t know where to find the Activity we’re trying to run (remember that Intents can start Activities from any application installed on the device). Let’s remedy this by registering our Activity within the application manifest file.
Open your application’s manifest file, AndroidManifest.xml in Eclipse, and switch to the code view by selecting the “AndroidManifest.xml” tab along the bottom of the window.
The application’s manifest file is where you define global settings and information about your application. You’ll see that it already declares .BrewClockActivity as the Activity to run when the application is launched.
Within , add a new node to describe the “Add Tea” Activity. Use the same add_tea_label string that we declared earlier in strings.xml for the Activity’s title:
2<application …>
4  <activity android:name=".AddTeaActivity" android:label="@string/add_tea_label" />
Save the manifest file before running BrewClock again. This time, when you open the menu and tap “Add Tea,” Android will start the AddTeaActivity. Hit the “Back” button to go back to the main screen.
With the Activities hooked together, it’s time to build an interface for adding tea!

Building The Tea Editor Interface

Building the interface to add a tea is very similar to how we built the main BrewClock interface in the previous tutorial. Start by creating a new layout file, and then add the appropriate XML, as below.
Alternatively, you could use Android’s recently improved layout editor in Eclipse to build a suitable interface. Create a new XML file in which to define the layout. Go to File → New, then select “Android XML File,” and select a “Layout” type. Name the file add_tea.xml.
11 New Layout Xml in Get Started Developing For Android With Eclipse, Reloaded
Replace the contents of add_tea.xml with the following layout:
02xml version="1.0" encoding="utf-8"?>
05  android:layout_width="fill_parent"
06  android:layout_height="fill_parent"
07  android:orientation="vertical"
08  android:padding="10dip">
10  <TextView
11    android:text="@string/tea_name_label"
12    android:layout_width="fill_parent"
13    android:layout_height="wrap_content" />
15  <EditText
16    android:id="@+id/tea_name"
17    android:layout_width="fill_parent"
18    android:layout_height="wrap_content"/>
20  <TextView
21    android:text="@string/brew_time_label"
22    android:layout_width="wrap_content"
23    android:layout_height="wrap_content"/>
25  <SeekBar
26    android:id="@+id/brew_time_seekbar"
27    android:layout_width="fill_parent"
28    android:layout_height="wrap_content"
29    android:progress="2"
30    android:max="9" />
32  <TextView
33    android:id="@+id/brew_time_value"
34    android:text="3 m"
35    android:textSize="20dip"
36    android:layout_width="fill_parent"
37    android:layout_height="wrap_content"
38    android:gravity="center_horizontal" />
We’ll also need to add some new strings to strings.xml for the labels used in this interface:
4  <string name="tea_name_label">Tea Namestring>
6  <string name="brew_time_label">Brew Timestring>
In this layout, we’ve added a new type of interface widget, the SeekBar. This lets the user easily specify a brew time by dragging a thumb from left to right. The range of values that the SeekBar produces always runs from zero (0) to the value of android:max.
In this interface, we’ve used a scale of 0 to 9, which we will map to brew times of 1 to 10 minutes (brewing for 0 minutes would be a waste of good tea!). First, though, we need to make sure that AddTeaActivity loads our new interface:
Add the following line of code to the Activity’s onCreate() method that loads and displays the add_tea layout file:
1// src/com/example/brewclock/
2public void onCreate(Bundle savedInstanceState) {
3  super.onCreate(savedInstanceState);
4  setContentView(R.layout.add_tea);
Now test your application by running it, pressing the “Menu” button and tapping “Add Tea” from the menu.
12 Add Tea Interface in Get Started Developing For Android With Eclipse, Reloaded
You’ll see your new interface on the “Add Tea” screen. You can enter text and slide the SeekBar left and right. But as you’d expect, nothing works yet because we haven’t hooked up any code.
Declare some properties in AddTeaActivity to reference our interface elements:
01// src/com/example/brewclock/
02public class AddTeaActivity {
03  // …
05  /** Properties **/
06  protected EditText teaName;
07  protected SeekBar brewTimeSeekBar;
08  protected TextView brewTimeLabel;
10  // …
Next, connect those properties to your interface:
1public void onCreate(Bundle savedInstanceState) {
2  // …
3  // Connect interface elements to properties
4  teaName = (EditText) findViewById(;
5  brewTimeSeekBar = (SeekBar) findViewById(;
6  brewTimeLabel = (TextView) findViewById(;
The interface is fairly simple, and the only events we need to listen for are changes to the SeekBar. When the user moves the SeekBar thumb left or right, our application will need to read the new value and update the label below with the selected brew time. We’ll use a Listener to detect when the SeekBar is changed:
Add an onSeekBarChangedListener interface to the AddTeaActivity class declaration, and add the required methods:
01// src/com/example/brewclock/
02public class AddTeaActivity
03extends Activity
04implements OnSeekBarChangeListener {
05  // …
07  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
08    // TODO Detect change in progress
09  }
11  public void onStartTrackingTouch(SeekBar seekBar) {}
13  public void onStopTrackingTouch(SeekBar seekBar) {}
The only event we’re interested in is onProgressChanged, so we need to add the code below to that method to update the brew time label with the selected value. Remember that our SeekBar values range from 0 to 9, so we’ll add 1 to the supplied value so that it makes more sense to the user:
Add the following code to onProgressChanged() in
1// src/com/example/brewclock/
2public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
3  if(seekBar == brewTimeSeekBar) {
4    // Update the brew time label with the chosen value.
5    brewTimeLabel.setText((progress + 1) + " m");
6  }
Set the SeekBar’s listener to be our AddTeaActivity in onCreate:
1// src/com/example/brewclock/
2public void onCreate(Bundle savedInstanceState) {
3  // …
5  // Setup Listeners
6  brewTimeSeekBar.setOnSeekBarChangeListener(this);
Now when run the application and slide the SeekBar left to right, the brew time label will be updated with the correct value:
13 Seekbar in Get Started Developing For Android With Eclipse, Reloaded

Saving Tea

With a working interface for adding teas, all that’s left is to give the user the option to save their new tea to the database. We’ll also add a little validation to the interface so that the user cannot save an empty tea to the database!
Start by opening strings.xml in the editor and adding some new labels for our application:
2<string name="save_tea_label">Save Teastring>
3<string name="invalid_tea_title">Tea could not be saved.string>
5<string name="invalid_tea_no_name">Enter a name for your tea.string>
Just like before, we’ll need to create a new options menu for AddTeaActivity so that the user can save their favorite tea:
Create a new XML file, add_tea.xml, in the res/menus folder by choosing File → New and then Other → Android XML File. Remember to select “Menu” as the resource type.
Add an item to the new menu for saving the tea:
2xml version="1.0" encoding="utf-8"?>
4  <item android:title="@string/save_tea_label" android:id="@+id/save_tea" />
Back in AddTeaActivity, add the override methods for onCreateOptionsMenu and onOptionsItemSelected, just like you did in BrewClockActivity. However, this time, you’ll supply the add_tea.xml resource file to the MenuInflater:
01// src/com/example/brewclock/
03public boolean onCreateOptionsMenu(Menu menu) {
04  MenuInflater inflater = getMenuInflater();
05  inflater.inflate(, menu);
07  return true;
11public boolean onOptionsItemSelected(MenuItem item) {
12  switch(item.getItemId()) {
13    case
14      saveTea();
16    default:
17      return super.onOptionsItemSelected(item);
18  }
Next, we’ll add a new method, saveTea(), to handle saving the tea. The saveTea method first reads the name and brew time values chosen by the user, validates them and (if all is well) saves them to the database:
01// src/com/example/brewclock/
02public boolean saveTea() {
03  // Read values from the interface
04  String teaNameText = teaName.getText().toString();
05  int brewTimeValue = brewTimeSeekBar.getProgress() + 1;
07  // Validate a name has been entered for the tea
08  if(teaNameText.length() < 2) {
09    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
10    dialog.setTitle(R.string.invalid_tea_title);
11    dialog.setMessage(R.string.invalid_tea_no_name);
14    return false;
15  }
17  // The tea is valid, so connect to the tea database and insert the tea
18  TeaData teaData = new TeaData(this);
19  teaData.insert(teaNameText, brewTimeValue);
20  teaData.close();
22  return true;
This is quite a hefty chunk of code, so let’s go over the logic.
First, we read the values of the EditText teaName and the SeekBar brewTimeSeekBar (remembering to add 1 to the value to ensure a brew time of between 1 and 10 minutes). Next, we validate that a name has been entered that is two or more characters (this is really simple validation; you might want to experiment doing something more elaborate, such as using regular expressions).
If the tea name is not valid, we need to let the user know. We make use of one of Android’s helper classes, AlertDialog.Builder, which gives us a handy shortcut for creating and displaying a modal dialog window. After setting the title and error message (using our string resources), the dialogue is displayed by calling its show() method. This dialogue is modal, so the user will have to dismiss it by pressing the “Back” key. At this point, we don’t want to save any data, so just return false out of the method.
If the tea is valid, we create a new temporary connection to our tea database using the TeaData class. This demonstrates the advantage of abstracting your database access to a separate class: you can access it from anywhere in the application!
After calling teaData.insert() to add our tea to the database, we no longer need this database connection, so we close it before returning true to indicate that the save was successful.
Try this out by running the application in the emulator, pressing “Menu” and tapping “Add Tea.” Once on the “Add Tea” screen, try saving an empty tea by pressing “Menu” again and tapping “Save Tea.” With your validation in place, you’ll be presented with an error message:
14 Invalid Tea in Get Started Developing For Android With Eclipse, Reloaded
Next, try entering a name for your tea, choosing a suitable brew time, and choosing “Save Tea” from the menu again. This time, you won’t see an error message. In fact, you’ll see nothing at all.

Improving the User Experience

While functional, this isn’t a great user experience. The user doesn’t know that their tea has been successfully saved. In fact, the only way to check is to go back from the “Add Tea” Activity and check the list of teas. Not great. Letting the user know that their tea was successfully saved would be much better. Let’s show a message on the screen when a tea has been added successfully.
We want the message to be passive, or non-modal, so using an AlertDialog like before won’t help. Instead, we’ll make use of another popular Android feature, the Toast.
Toasts display a short message near the bottom of the screen but do not interrupt the user. They’re often used for non-critical notifications and status updates.
Start by adding a new string to the strings.xml resource file. Notice the %s in the string? We’ll use this in the next step to interpolate the name of the saved tea into the message!
2<string name="save_tea_success">%s tea has been saved.string>
Modify the code in onOptionsItemSelected to create and show a Toast pop-up if the result of saveTea() is true. The second parameter uses of getString() interpolate the name of our tea into the Toast message. Finally, we clear the “Tea Name” text so that the user can quickly add more teas!
1// src/com/example/brewclock/
2// …
3switch(item.getItemId()) {
4 case
5   if(saveTea()) {
6     Toast.makeText(this, getString(R.string.save_tea_success, teaName.getText().toString()), Toast.LENGTH_SHORT).show();
7     teaName.setText("");
8   }
9// …
Now re-run your application and try adding and saving a new tea. You’ll see a nice Toast pop up to let you know the tea has been saved. The getString() method interpolates the name of the tea that was saved into the XML string, where we placed the %s.
16 Valid Save in Get Started Developing For Android With Eclipse, Reloaded
Click the “Back” button to return to the application’s main screen, and tap the tea spinner. The new teas you added in the database now show up as options in the spinner!

User Preferences

BrewClock is now fully functional. Users can add their favorite teas and the respective brewing times to the database, and they can quickly select them to start a new brew. Any teas added to BrewClock are saved in the database, so even if we quit the application and come back to it later, our list of teas is still available.
One thing you might notice when restarting BrewClock, though, is that the brew counter is reset to 0. This makes keeping track of our daily tea intake (a vital statistic!) difficult. As a final exercise, let’s save the total brew count to the device.
Rather than adding another table to our teas database, we’ll make use of Android’s “Shared Preferences,” a simple database that Android provides to your application for storing simple data (strings, numbers, etc.), such as high scores in games and user preferences.
Start by adding a couple of constants to the top of These will store the name of your shared preferences file and the name of the key we’ll use to access the brew count. Android takes care of saving and persisting our shared preferences file.
1// src/com/example/brewclock/
2protected static final String SHARED_PREFS_NAME = "brew_count_preferences";
3protected static final String BREW_COUNT_SHARED_PREF = "brew_count";
Next, we’ll need to make some changes to the code so that we can read and write the brew count to the user preferences, rather than relying on an initial value in our code. In BrewClockActivity’s onCreate method, change the lines around setBrewCount(0) to the following:
01// src/com/example/brewclock/
02public void onCreate() {
03  // …
05  // Set the initial brew values
06  SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE);
07  brewCount = sharedPreferences.getInt(BREW_COUNT_SHARED_PREF, 0);
08  setBrewCount(brewCount);
10  // …
Here we’re retrieving an instance of the application’s shared preferences using SharedPreferences, and asking for the value of the brew_count key (identified by the BREW_COUNT_SHARED_PREF constant that was declared earlier). If a value is found, it will be returned; if not, we’ll use the default value in the second parameter of getInt (in this case, 0).
Now that we can retrieve the stored value of brew count, we need to ensure its value is saved to SharedPreferences whenever the count is updated.
Add the following code to setBrewCount in BrewClockActivity:
01// src/com/example/brewclock/
02 public void setBrewCount(int count) {
03   brewCount = count;
04   brewCountLabel.setText(String.valueOf(brewCount));
06   // Update the brewCount and write the value to the shared preferences.
07   SharedPreferences.Editor editor = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE).edit();
08   editor.putInt(BREW_COUNT_SHARED_PREF, brewCount);
09   editor.commit();
10 }
Shared preferences are never saved directly. Instead, we make use of Android’s SharedPreferences.Editor class. Calling edit() on SharedPreferences returns an editor instance, which can then be used to set values in our preferences. When the values are ready to be saved to the shared preferences file, we just call commit().
With our application’s code all wrapped up, it’s time to test everything!
Run the application on the emulator, and time a few brews (this is the perfect excuse to go and make a well-deserved tea or two!), and then quit the application. Try running another application that is installed on the emulator to ensure BrewClock is terminated. Remember that Android doesn’t terminate an Activity until it starts to run out of memory.
When you next run the application, you’ll see that your previous brew count is maintained, and all your existing teas are saved!


Congratulations! You’ve built a fully working Android application that makes use of a number of core components of the Android SDK. In this tutorial, you have seen how to:
  • Create a simple SQLite database to store your application’s data;
  • Make use of Android’s database classes and write a custom class to abstract the data access;
  • Add option menus to your application;
  • Create and register new Activities within your application and bind them together into a coherent interface using Intents;
  • Store and retrieve simple user data and settings using the built-in “Shared Preferences” database.
Data storage and persistence is an important topic, no matter what type of application you’re building. From utilities and business tools to 3-D games, nearly every application will need to make use of the data tools provided by Android.
17 Brew Up in Get Started Developing For Android With Eclipse, Reloaded


BrewClock is now on its way to being a fully functional application. However, we could still implement a few more features to improve the user experience. For example, you might like to develop your skills by trying any of the following:
  • Checking for duplicate tea name entries before saving a tea,
  • Adding a menu option to reset the brew counter to 0,
  • Storing the last-chosen brew time in a shared preference so that the application defaults to that value when restarted,
  • Adding an option for the user to delete teas from the database.
Solutions for the Activities will be included in a future branch on the GitHub repository, where you’ll find the full source-code listings. You can download the working tutorial code by switching your copy of the code to the tutorial_2 branch:
1# If you’ve not already cloned the repository,
2# you’ll need to do that first:
4# $ cd BrewClock
5$ git checkout tutorial_2
I hope you’ve enjoyed working through this tutorial and that it helps you in designing and building your great Android applications. Please let me know how you get on in the comments below, or feel free to drop me an email.
  1. resourceful article you have shared thank you

  2. This was so useful and informative about. Thanks for sharing.
    indian restaurants in hong kong

  3. We have earned this unshakable trust and reliability Escorts contact number in Ahmedabad from my customers in the light of the excellence of my services that has made me one Escorts in Ahmedabad of the most adored Low VIP Call Girls in Gurgaon budget Escorts in Ahmedabad.Escorts whatsapp number in Goa We strongly believe in business behavior that entirely depends upon credibility and the promptness of the services.Escorts whatsapp number in Goa This is the reason we have maintained a top position among all the female adult entertainers in Dating Escorts in Dehradunthe city of Ahmedabad.


Start typing and press Enter to search