Get Started Developing for Android with Eclipse

Advertisement

There’s a lot to get excited about in mobile application development today. With increasingly sophisticated hardware, tablet PCs and a variety of software platforms (Symbian OS, iOS, WebOS, Windows Phone 7…), the landscape for mobile developers is full of opportunities — and a little complex as well.

So much choice can be overwhelming when you just want to get started building mobile applications. Which platform should you choose? What programming language should you learn? What kit do you need for your planned project? In this tutorial, you’ll learn how to start writing applications for Android, the open-source mobile operating system popularized by Google.

Why Develop for Android?

Android is an open-source platform based on the Linux kernel, and is installed on thousands of devices from a wide range of manufacturers. Android exposes your application to all sorts of hardware that you’ll find in modern mobile devices — digital compasses, video cameras, GPS, orientation sensors, and more.

Android’s free development tools make it possible for you to start writing software at little or no cost. When you’re ready to show off your application to the world, you can publish it to Google’s Android Market. Publishing to Android Market incurs a one-off registration fee (US $25 at the time of writing) and, unlike Apple’s App Store which famously reviews each submission, makes your application available for customers to download and buy after a quick review process — unless the application is blatantly illegal.

Here are a few other advantages Android offers you as a developer:

  • The Android SDK is available for Windows, Mac and Linux, so you don’t need to pay for new hardware to start writing applications.
  • An SDK built on Java. If you’re familiar with the Java programming language, you’re already halfway there.
  • By distributing your application on Android Market, it’s available to hundreds of thousands of users instantly. You’re not just limited to one store, because there are alternatives, too. For instance, you can release your application on your own blog. Amazon have recently been rumoured to be preparing their own Android app store also.
  • As well as the technical SDK documentation, new resources are being published for Android developers as the platform gains popularity among both users and developers.

Enough with the talk — let’s get started developing for Android!

Installing Eclipse and the Android SDK

The recommended environment for developing Android applications is Eclipse with the Android Development Toolkit (ADT) plugin installed. I’ll summarize the process here. If you need more detail, Google’s own developer pages do a good job of explaining the installation and configuration process.

  • Download the Android SDK for your platform (Windows, Mac OS X, or Linux).
  • Extract the downloaded file to somewhere memorable on your hard drive (on Linux, I use /opt/local/).
  • If you don’t already have Eclipse installed, download and install the Eclipse IDE for Java Developers package. For programming, Google recommends using Eclipse 3.5 (Galileo).
  • Run Eclipse and choose Help->Install New Software.
  • Click Add in the Available Software window.
  • Enter Android Development Tools in the Name field, and https://dl-ssl.google.com/android/eclipse/ in the Location field.
  • Click OK and check Developer Tools in the list of available software. This will install the Android Development Tools and DDMS, Android’s debugging tool.
  • Screensot
    Large image

  • Click Next and Finish to install the plugin. You’ll need to restart Eclipse once everything is installed.
  • When Eclipse restarts, choose Window->Preferences and you should see Android listed in the categories.
  • You now need to tell Eclipse where you’ve installed the Android SDK. Click Android and then Browse to select the location where you extracted the SDK files. For example, /opt/local/android-sdk.
    Configuring ADT
    Large view
  • Click OK to have Eclipse save the location of your SDK.

Targeting Android Platforms

Before you can start writing applications for Android, you need to download the SDK platforms for the Android devices for which you want to develop apps. Each platform has a different version of the Android SDK that may be installed on users’ devices. For versions of Android 1.5 and above, there are two platforms available: Android Open Source Project and Google.

The Android Open Source Project platforms are open source, but do not include Google’s proprietary extensions such as Google Maps. If you choose not to use the Google APIs, Google’s mapping functionality won’t be available to your application. Unless you have a specific reason not to, I’d recommended you to target one of the Google platforms, as this will allow you to take advantage of Google’s proprietary extensions.

  • Choose Window->Android SDK and AVD Manager.
  • Click Available Packages in the left column and check the repository to show a list of the available Android platforms.
  • You can choose which platforms to download from the list, or leave everything checked to download all the available platforms. When you’re done, click Install Selected and follow the installation instructions.
    Large image

Once everything has been successfully downloaded, you’re ready to start developing for Android.

Creating a New Android Project

Eclipse’s New Project Wizard can create a new Android application for you, generating files and code that are ready to run right out of the box. It’s a quick way to see something working, and a good starting point from which to develop your own applications:

  • Choose File->New->Project…
  • Choose Android Project
  • In the New Project dialog, enter the following settings:
    Project Name: BrewClock
    Build Target: Google Inc. 1.6 (Api Level 4)
    Application Name: BrewClock
    Package Name: com.example.brewclock
    Create Activity: BrewClockActivity
    Min SDK Version: 4

After clicking Finish, Eclipse will create a new Android project that’s ready to run. Notice you told Eclipse to generate an Activity called BrewClockActivity? This is the code that Android actually uses to run your application. The generated code will display a simple ‘Hello World’ style message when the application runs.

Packages

The package name is an identifier for your application. When the time comes and you are willing to publish on Android Market, it’s exactly this identifier that will be used to track your application for updates, so it’s important to make sure it’s unique. Although we’re using the com.example.brewclock namespace here, for a real application it’s best to choose something like com.yourcompanyname.yourapplication.

SDK Versions

The Min SDK Version is the earliest version of Android on which your application will run. With each new release of Android, the SDK adds and changes methods. By choosing an SDK version, Android (and the Android Market) knows that your application will only run on devices with a version of Android later or equal than the specified version.

Running Your Application

Now let’s try running the application in Eclipse. As this is the first run, Eclipse will ask what type of project you are working on:

  • Choose Run->Run or press Ctrl+F11.
  • Choose Android Application and click OK.

Eclipse will now try to run the application on an Android device. At the moment, though, you don’t have any Android devices running, so the run will fail and you’ll be asked to create a new Android Virtual Device (AVD).

Android Virtual Devices

An Android Virtual Device (AVD) is an emulator that simulates a real-world Android device, such as a mobile phone or Tablet PC. You can use AVDs to test how your application performs on a wide variety of Android devices, without having to buy every gadget on the market.

You can create as many AVDs as you like, each set up with different versions of the Android Platform. For each AVD you create, you can configure various hardware properties such as whether it has a physical keyboard, GPS support, the camera resolution, and so on.

Before you can run your application, you need to create your first AVD running the target SDK platform (Google APIs 1.6).

Let’s do that now:

  • If you haven’t tried to run your application yet, click Run now (or hit Ctrl+F11)
  • When the target device warning pops up, click Yes to create a new AVD.
  • Click New in the Android SDK and AVD Manager dialog that appears.
  • Enter the following settings for the AVD:
    Name: Android_1.6
    Target: Google APIs (Google Inc.) - API Level 4
    SD Card Size: 16 MiB
    Skin Built In: Default (HVGA)
  • Click Create AVD to have Android build your new AVD.
  • Close the Android SDK and AVD Manager dialog.

Running the Code

Try running your application again (Ctrl+F11). Eclipse will now build your project and launch the new AVD. Remember, the AVD emulates a complete Android system, so you’ll even need to sit through the slow boot process just like a real device. For this reason, once the AVD is up and running, it’s best not to close it down until you’ve finished developing for the day.

When the emulator has booted, Eclipse automatically installs and runs your application:


Large image

Building Your First Android Application

Testing generated code is all well and good, but you want to start building a real application. For this, we’ll step through a simple design process and build an application that you can deploy to your Android device.

Most developers (myself included) like a constant supply of good tea or coffee. In the next section of this article you’ll build a simple tea counter application to track how many cups of tea (brews) the user has drunk, and let them set a timer for brewing each cup.

You can download the complete code for this tutorial on GitHub.

Designing the User Interface

One of the first steps to building any Android application is to design and build the user interface. Here’s a quick sketch of how the application’s interface will look:


Large image

The user will be able to set a brew time in minutes using the + and - buttons. When they click Start, a countdown will start for the specified number of minutes. Unless the user cancels the brew by tapping the button again, the brew count will be increased when the countdown timer reaches 0.

Building the Interface

Android user interfaces, or layouts, which are described in XML documents, can be found in the res/layouts folder. The template code that Eclipse generated already has a simple layout declared in res/layouts/main.xml which you may have seen previously while the application was running on the emulator.

Eclipse has a graphical layout designer that lets you build the interface by ‘dragging’ and ‘dropping’ controls around the screen. However, I often find it easier to write the interface in XML and use the graphical layout to preview the results.

Let’s do this now by changing main.xml to match the design sketch above:

  • Open res/layouts/main.xml in Eclipse by double-clicking it in the Package Explorer.
  • Click the main.xml tab along the bottom of the screen to switch to XML view.

Now change the content of main.xml to:

# /res/layouts/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip">
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="20dip"
      android:text="Brews: " />
    <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="None"
      android:gravity="right"
      android:textSize="20dip"
      android:id="@+id/brew_count_label" />
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:padding="10dip">
    <Button
      android:id="@+id/brew_time_down"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="-"
      android:textSize="40dip" />
    <TextView
      android:id="@+id/brew_time"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="0:00"
      android:textSize="40dip"
      android:padding="10dip" />
    <Button
      android:id="@+id/brew_time_up"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="+"
      android:textSize="40dip" />
  </LinearLayout>
  <Button
    android:id="@+id/brew_start"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:text="Start" />
</LinearLayout>

As you can see, Android’s XML layout files are verbose, but allow you to control virtually every aspect of elements on the screen.

One of the most important interface elements in Android are Layout containers, such as the LinearLayout used in this example. These elements are invisible to the user but act as layout containers for other elements such as Buttons and TextViews.

There are several types of layout views, each of which is used to build different types of layout. As well as the LinearLayout and AbsoluteLayout, the TableLayout allows the use of complex grid-based interfaces. You can find out more about Layouts in the Common Layout Objects section of the API documents.

Linking Your Layout With Code

After saving your layout, try running your application in the emulator again by pressing Ctrl+F11, or clicking the Run icon in Eclipse. Now instead of the ‘Hello World’ message you saw earlier, you’ll see Android now displays your application’s new interface.

If you click any of the buttons, they’ll highlight as expected, but don’t do anything yet. Let’s remedy that by writing some code behind the interface layout:

# /src/com/example/brewclock/BrewClockActivity.java
...
import android.widget.Button;
import android.widget.TextView;

public class BrewClockActivity extends Activity {
  /** Properties **/
  protected Button brewAddTime;
  protected Button brewDecreaseTime;
  protected Button startBrew;
  protected TextView brewCountLabel;
  protected TextView brewTimeLabel;

  ...
 }

Next, we’ll change the call to onCreate. This is the method that gets called whenever Android starts your application. In the code that Eclipse generated, onCreate sets the activity’s view to be R.layout.main. It’s that line of code that tells Android to decode our layout XML document and display it to the user.

The Resource Object

In Android, R is a special object that is automatically generated to allow access to your project’s resources (layouts, strings, menus, icons…) from within the code. Each resource is given an id. In the layout file above, these are the @+id XML attributes. We’ll use those attributes to connect the Buttons and TextViews in our layout to the code:

# /src/com/example/brewclock/BrewClockActivity.java
...
public class BrewClockActivity extends Activity {
  ...
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Connect interface elements to properties
    brewAddTime = (Button) findViewById(R.id.brew_time_up);
    brewDecreaseTime = (Button) findViewById(R.id.brew_time_down);
    startBrew = (Button) findViewById(R.id.brew_start);
    brewCountLabel = (TextView) findViewById(R.id.brew_count_label);
    brewTimeLabel = (TextView) findViewById(R.id.brew_time);
  }
}

Listening For Events

In order to detect when the user taps one of our buttons, we need to implement a listener. You may be familiar with listeners or callbacks from other event-driven platforms, such as Javascript/jQuery events or Rails’ callbacks.

Android provides a similar mechanism by providing Listener interfaces, such as OnClickListener, that define methods to be triggered when an event occurs. Implementing the OnClickListener interface will notify your application when the user taps the screen, and on which button they tapped. You also need to tell each button about the ClickListener so that it knows which listener to notify:

# /src/com/example/brewclock/BrewClockActivity.java
...
// Be sure not to import
// `android.content.dialoginterface.OnClickListener`.
import android.view.View.OnClickListener; 

public class BrewClockActivity extends Activity
  implements OnClickListener {
  ...
  public void onCreate(Bundle savedInstanceState) {
    ...
    // Setup ClickListeners
    brewAddTime.setOnClickListener(this);
    brewDecreaseTime.setOnClickListener(this);
    startBrew.setOnClickListener(this);
  }
  ...
  public void onClick(View v) {
    // TODO: Add code to handle button taps
  }
}

Next we’ll add code that handles each of our button presses. We’ll also add four new properties to the Activity that will let the user set and track the brewing time, how many brews have been made, and whether the timer is currently running.

# /src/com/example/brewclock/BrewClockActivity.java
...
public class BrewClockActivity extends Activity
  implements OnClickListener {
  ...
  protected int brewTime = 3;
  protected CountDownTimer brewCountDownTimer;
  protected int brewCount = 0;
  protected boolean isBrewing = false;
  ...
  public void onClick(View v) {
    if(v == brewAddTime)
      setBrewTime(brewTime + 1);
    else if(v == brewDecreaseTime)
      setBrewTime(brewTime -1);
    else if(v == startBrew) {
      if(isBrewing)
        stopBrew();
      else
        startBrew();
    }
  }
}

Notice we’re using the CountDownTimer class provided by Android. This lets you easily create and start a simple countdown, and be notified at regular intervals whilst the countdown is running. You’ll use this in the startBrew method below.

The following methods are all model logic that handles setting the brew time, starting and stopping the brew and maintaining a count of brews made. We’ll also initialize the brewTime and brewCount properties in onCreate.

It would be good practice to move this code to a separate model class, but for simplicity we’ll add the code to our BrewClockActivity:

# /src/com/example/brewclock/BrewClockActivity.java
...
public class BrewClockActivity extends Activity
  implements OnClickListener {
  ...
  public void onCreate(Bundle savedInstanceState) {
    ...
    // Set the initial brew values
    setBrewCount(0);
    setBrewTime(3);
  }

  /**
   * Set an absolute value for the number of minutes to brew.
   * Has no effect if a brew is currently running.
   * @param minutes The number of minutes to brew.
   */
  public void setBrewTime(int minutes) {
    if(isBrewing)
      return;

    brewTime = minutes;

    if(brewTime < 1)
      brewTime = 1;

    brewTimeLabel.setText(String.valueOf(brewTime) + "m");
  }

  /**
   * Set the number of brews that have been made, and update
   * the interface.
   * @param count The new number of brews
   */
  public void setBrewCount(int count) {
    brewCount = count;
    brewCountLabel.setText(String.valueOf(brewCount));
  }

  /**
   * Start the brew timer
   */
  public void startBrew() {
    // Create a new CountDownTimer to track the brew time
    brewCountDownTimer = new CountDownTimer(brewTime * 60 * 1000, 1000) {
      @Override
      public void onTick(long millisUntilFinished) {
        brewTimeLabel.setText(String.valueOf(millisUntilFinished / 1000) + "s");
      }

      @Override
      public void onFinish() {
        isBrewing = false;
        setBrewCount(brewCount + 1);

        brewTimeLabel.setText("Brew Up!");
        startBrew.setText("Start");
      }
    };

    brewCountDownTimer.start();
    startBrew.setText("Stop");
    isBrewing = true;
  }

  /**
   * Stop the brew timer
   */
  public void stopBrew() {
    if(brewCountDownTimer != null)
      brewCountDownTimer.cancel();

    isBrewing = false;
    startBrew.setText("Start");
  }
  ...
}

The only parts of this code specific to Android are setting the display labels using the setText method. In startBrew, we create and start a CountDownTimer to start counting down every second until a brew is finished. Notice that we define CountDownTimer's listeners (onTick and onFinish) inline. onTick will be called every 1000 milliseconds (1 second) the timer counts down, whilst onFinish is called when the timer reaches zero.

Avoiding Hard-Coded Text in your Code

To keep this tutorial code simple, I’ve intentionally written label strings directly in the code (e.g. "Brew Up!", "Start", "Stop"). Generally, this isn’t good practice, as it makes finding and changing those strings harder in large projects.

Android provides a neat way to keep your text strings separate from code with the R object. R lets you define all your application’s strings in an xml file (res/values/strings.xml) which you can then access in code by reference. For example:

# /res/values/strings.xml
<string name="brew_up_label">Brew Up!</string>
...

# /res/com/example/brewclock/BrewClockActivity.java
...
brewLabel.setText(R.string.brew_up_label);
...

Now if you wanted to change Brew Up! to something else, you would only need to change it once in the strings.xml file. Your application starts to span dozens of code files which keeps all your strings in one place and makes a lot of sense!

Trying BrewClock

With the code complete, it’s time to try out the application. Hit Run or Ctrl+F11 to start BrewClock in the emulator. All being well, you’ll see the interface set up and ready to time your tea brewing! Try setting different brew times, and pressing Start to watch the countdown.


Large image

Summary

In this short introduction to Android, you’ve installed the Android SDK and Eclipse Android Development Tools (ADT) plugin. You’ve set up an emulator, or virtual device that can test your applications. You’ve also built a working Android application which has highlighted a number of key concepts that you’ll use when developing your own Android applications.

Hopefully, this has whet your appetite for building mobile applications, and experimenting in this exciting field. Android offers a great way to start writing applications for a range of current and upcoming mobile devices. If you’ve built or are working on your own mobile app, be sure to let us know about it in the comments!

(ik), (vf)

↑ Back to top

Chris is a software developer working with Ruby, Rails and Android. In 2010, he founded Plymouth Software where he designs and builds applications for the web and mobile devices. As well as a fondness for travel and drinking tea, Chris writes about code, design and business on his blog at chrisblunt.com.

  1. 1

    Great! Thank you!

    2
  2. 2

    Cool..Thanks

    0
  3. 3

    Great article !
    Like to see *a lot* more of this !

    Already followed a 1-day seminar on building Android apps, and it’s not that easy for someone like me (webdesigner/developer).
    Step-by-step tutorials of Smashing quality would come in handy indeed. (or another e-book maybe ? )

    19
  4. 4

    This is a really GREAT snapshot of android development! Smashing!!!

    6
  5. 5

    Great tutorial! Can’t wait to get started.

    0
  6. 6

    Sooo much thanks, this is a great tutorial for begginers!!!!

    Greetings from Colombia!!!

    2
  7. 7

    Sorry for diversion, but what desktop font are you using on Ubuntu? It’s really nice!

    1
  8. 8

    Sorry for ranting but I can’t let this slip. “An SDK is built on Java” should probably be “THIS SDK is built on Java”. Describing SDK as something that is written for Java is just… wrong, as an SDK could be for any language.

    -2
  9. 9

    Thanks to everyone for your comments!

    @Karl The font I’m using now is “Ubuntu”, the new default in 10.10 Maverick. The designers, DaltonMaag, have some other stunning fonts available.

    @Klesus Thanks for pointing out the typo, I’ve updated the post.

    0
  10. 10

    Just in time =D

    I think Android is getting everyday more and more developers, and that post have become just in the right time for me =D

    Maybe one day you guys start a new site, droidtuts+

    TKS!

    0
  11. 11

    Checkout the following Android tutorial: http://www.quesucede.com/page/show/id/conway_game_of_life_android.

    It walks the developer through building Conway’s Game of Life – the problem space is small enough to allow the developer to only focus on Google Android while still including sufficient elements to make for both an interesting and valid learning experience.

    Brett Kromkamp

    0
  12. 12

    Thanks for this article!
    Though I think I saw “Comic Sans” somewhere…
    =S

    1
  13. 13

    Thanks for this! How upward compatible are the different platforms? If I decide to write something for 1.6 (your example) will it run on 2.2 as well? I guess this shouldn’t be a problem but I’m completely new to Android so don’t shoot me :-)

    0
  14. 14

    Useful stuff, thanks :) I have to ask if you used any of the articles here: http://mikeyhogarth.wordpress.com/ as a reference? They cover a lot of the same points.

    0
  15. 15

    Sweet post Chris. Once devs are ready to publish and would like extra help on promoting their app(s) they should take a peek at the lightweight GamerShots SDK to include in their projects – http://gamershots.com/developer

    3
  16. 16

    @ZizzelDaZuz It depends on what features of the SDK your app uses, but my experience has been that Android has good upward-compatibility.

    I’ve run into minor problems such as changing file storage paths in 2.2, but the changes are very well-covered in the SDK docs. Your code can query the current device to discover platform and capabilities, and act appropriately.

    1
  17. 17

    A really clear, concise and well explained tutorial, which has proven to be extremely helpful as I’ve been struggling getting started with Android. More from this author please!

    3
  18. 18

    Just Awesome

    1
  19. 19

    Great article. Thank you.
    I’m just starting with Android… with mobile apps… and this was perfect timing.

    I have a few errors popping up in Eclipse. I think I followed your tutorial to the dot, but I’m not sure now. would it be possible for you to post the full contents of BrewClockActivity.java? This may help others, aswell :)

    0
  20. 20

    This is a great tutorial. Can you write a tutorial for Android development using PhoneGap next?

    0
  21. 21

    @pioSko: Thanks :) The source code for the tutorial is available at http://github.com/cblunt/brewclock

    @Scott: Not tried PhoneGap – looks interesting though!

    0
  22. 22

    I followed your tutorial and it is very useful, thank you very much
    I found a mistake, you writed
    brewLabel.setText(R.string.brew_up_label);
    but it should be
    brewTimeLabel.setText(R.string.brew_up_label);

    It is very complete, but I miss a point where you explain how to put the app image.

    A tip for the web developers interested in developing android apps, take a look to phonegap, it is very useful for creating “native” iphone and android apps using html, css and javascript (and even blackberry, wm and symbian)

    0
  23. 23

    I have a feeling this article was written to somehow express a “hate” toward Jobs’ recent prescon. Great response (slap) in his face.
    Keep up the good work SM.

    -1
  24. 24

    That’s nice!

    0
  25. 25

    Web Designer Houston

    October 25, 2010 8:06 pm

    This is a really GREAT snapshot of android development! Smashing!!!

    I totally agree with Tom. I’d love to see a lot more of these type of articles!
    Thanks for sharing this!!!

    0
  26. 26

    great article i really want to try Android :)

    0
  27. 27

    Thanks, really appreciate all the comments! :)

    @Laura Hadn’t seen that blog, but would like to add it to a list of useful resources for Android developers that I’m compiling.

    1
  28. 28

    Great article although i installed the Eclipse & Android on my PC but really cant build anything special i got inspired and get a direction on how to build a application after looking at your sample demo :)

    1
  29. 29

    Wow, this a great post….I’m interested in becoming an Android developer.
    Thanks…

    0
  30. 30

    Awesome tutorial. Thanks Chris!

    0
  31. 31

    Just what i was looking for. Thanks a lot!

    2
  32. 32

    Thank you very much for this! More Android stuff @ SM!

    1
  33. 33

    Thanks!
    Very useful article :)
    Looking fwd to more android tips

    Anoyne here knows how to connect HTC Hero 2.1 with IntelliJ Idea [windows] into debug mode? Win can`t detect phone in that mode :-/

    0
  34. 34

    This is a really useful article, thanks

    I have used Titanium by Appcelerator (http://www.appcelerator.com) to develop apps for Android and iPhone. It works well and is a good way to develop apps for multiple platforms. Maybe Smashing Mag can do an article on Titanium?

    0
  35. 35

    Brilliant article, more like this please! :)

    0
  36. 36

    Perfect, I was looking for a novice intro for Eclipse on Windows. Thanks again!

    2
  37. 37

    Thank you man !!! Very helpfull !!!

    0
  38. 38

    Thanks a lot for posting this tutorial, i would love to see more articles on Android development here.

    0
  39. 39

    Thanks a lot its so helpful tutorial.

    0
  40. 40

    very good tutorials for starting with.
    Thanks for sharing.:D

    0
  41. 41

    tx, any chance of converting this tutorial with Netbeans???

    0
  42. 42

    Hey Hi,

    Nice article.

    I am an amateur at Eclipse and android and I am getting this error on running your code.

    Java Model Exception: Java Model Status [gen [in BrewClock] does not exist]
    at org.eclipse.jdt.internal.core.JavaElement.newJavaModelException(JavaElement.java:502)
    at org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:246)
    at org.eclipse.jdt.internal.core.JavaElement.openWhenClosed(JavaElement.java:515)
    at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:252)
    at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:238)
    at org.eclipse.jdt.internal.core.PackageFragmentRoot.getKind(PackageFragmentRoot.java:477)
    at org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider.processDelta(PackageExplorerContentProvider.java:645)
    at org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider.handleAffectedChildren(PackageExplorerContentProvider.java:791)
    at org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider.processDelta(PackageExplorerContentProvider.java:734)
    at org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider.handleAffectedChildren(PackageExplorerContentProvider.java:791)
    at org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider.processDelta(PackageExplorerContentProvider.java:734)
    at org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider.elementChanged(PackageExplorerContentProvider.java:124)
    at org.eclipse.jdt.internal.core.DeltaProcessor$3.run(DeltaProcessor.java:1557)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.jdt.internal.core.DeltaProcessor.notifyListeners(DeltaProcessor.java:1547)
    at org.eclipse.jdt.internal.core.DeltaProcessor.firePostChangeDelta(DeltaProcessor.java:1381)
    at org.eclipse.jdt.internal.core.DeltaProcessor.fire(DeltaProcessor.java:1357)
    at org.eclipse.jdt.internal.core.DeltaProcessor.resourceChanged(DeltaProcessor.java:1958)
    at org.eclipse.jdt.internal.core.DeltaProcessingState.resourceChanged(DeltaProcessingState.java:470)
    at org.eclipse.core.internal.events.NotificationManager$2.run(NotificationManager.java:291)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.internal.events.NotificationManager.notify(NotificationManager.java:285)
    at org.eclipse.core.internal.events.NotificationManager.broadcastChanges(NotificationManager.java:149)
    at org.eclipse.core.internal.resources.Workspace.broadcastPostChange(Workspace.java:313)
    at org.eclipse.core.internal.resources.Workspace.endOperation(Workspace.java:1022)
    at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:153)
    at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:238)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

    It would be great if you could please help with this.

    Thanks,

    Ganesh.

    2
  43. 43

    Great article man! I’m dabbling in android development myself at the moment too. If you get a chance to write another article, one thing I’d really love to hear is a decent balanced argument on is whether there’s an e-commerce based future in app development. Personally, I think it’s going to be limited to games and applications rather than the whole “me too” boom that occurred with websites – the problem is that companies (in my experience) all *want* an app without stopping to think whether they *need* one. On balance I’d say that the future of mobile based e-commerce is going to be mobile optimized websites – everyone’s gonna want one of those in the next few years!

    Anyway I’ll stop rambling now. Keep up the good work dude!

    1
  44. 44

    Hey Hi,

    Thanks for the help.

    Just want to add that I always get this error when I create a new project.

    Failed to load properties file for project ‘BrewClock’

    Am i missing some step here?

    Please let me know.

    Thanks Again,

    Ganesh.

    0
  45. 45

    Great article Chris!

    -1
  46. 46

    This tutorial was very frustrating for me. I got the layout done and it works well, but then when I move on to the next part and type everything exactly as you have it, many of the things are underlined in red and it says there are errors. Also, I don’t understand the relationship between the different sets of code you have written.

    For example, when you said:

    “We’ll use those attributes to connect the Buttons and TextViews in our layout to the code:”

    What does that mean? It seems like every new code you start begins with “# /src/com/example/brewclock/BrewClockActivity.java ” so that confuses me a lot, are you overwriting the code you put in previously? It seems like you are writing over the old code since it has the same line numbers (1-22). Or am I creating multiple pages of code? Please help.

    3
  47. 47

    @Philip Regarding your “attributes connect the buttons etc..” question, you can define your layout either computationally or using XML – if you go for the latter (and, to facilitate a decent seperation of concerns it’s reccomended you do) then you need to get references to the elements from your XML in your source code if you want to perform any interactions on them. If you’re from a web background, it’s the exact same principle as defining an ID for an HTML element and then accesing it from javascript using “document.getElementByID” or “$(#element)”.

    I personally think this article is pretty good, and I really ordinarily wouldn’t do this, but I have been writing articles myself on android which cover almost the same points as this one does but go through everything a bit more slowly and in a bit more detail. Someone’s already linked to it above but here it is again.

    http://mikeyhogarth.wordpress.com/articles/

    If linking to external articles / your own work is frowned upon by this community (don’t really post here that often) then please accept my apologies and feel free to moderate or delete this post.

    -Mikey xx

    1
  48. 48

    Unfortunately, I am already using Eclipse Helios (J2EE developers). I installed the Android Development tools in Eclipse but when I open windows/preferences android does not appear as an option.

    So I am kind of stuck at the first hurdle :(

    Anyone any ideas – OS Linux/Ubuntu

    0
  49. 49

    Great article :)

    Give us more like that ;)

    0
  50. 50

    I dont want to spoil this article , but for everyone new to android and not knowing anything abouth java,or any code related, for these persons i have a verry good and fun to use program made by some google team members.
    It is called Appinventor (AI) , it is a Google Beta program what requires inventation.I’m one of the few but growing beta users.

    Anyway forget my bad englisch and take a look:
    http://appinventor.googlelabs.com/about/

    PS:I dont work for google, i asked several times if they had a job for me but never got a response lol

    Grtzzz wHiTeHaT

    0
  51. 51

    Hi Chris:

    Thanks for this fantastic article.
    I’ve never seen a more straight forward tutorial on anything!
    This is surely a talent in itself, which I am sure, comes from years of doing what you are passionate about!

    Hoping to see more content from you!

    Cheers and all the best for Plymouth Software!
    Ash

    0
  52. 52

    Hi,

    I was able to get my initial app up and running following the directions, but don’t see any Hello World message. Did I miss a step? I would have expected to have written the Hello World text in the Activity class but didn’t see directions for that.

    Cory

    0
  53. 53

    Thanks a lot, Chris.
    After this post I think I’ll finally start some Android development.
    I’ve completed this tutorial using IDEA and it works well too!
    I’ve noticed some mistakes (or maybe these are not mistakes in Android SDK) – you’re using == operator to check which button has been clicked. I’m not sure as it works in Android, but in Java SDK this should be better replaced with .equals() – this will guarantee the correctness. And again – I’m just thinking as Java developer – I’m not an Android developer yet and I don’t know how the == is treated in Android platforms

    0
  54. 54

    Thanks Chris..

    All instructions were a perfect spot on…followed it step by step and it worked without a hitch…

    0
  55. 55

    Since having my android-running HTC Legend, I always wondered how to build apps myself. Having a lot of other things to be done currently, I didn’t follow this anymore, thinking it’s a hard way to get into this coding stuff based on Android. You need to know I’m programming for long in PHP and other website languages, as well as in Java for 3 years.
    I jumped into this article randomly, and it was mindblowing! I had never thought it’s that easy (and fun) developing for Android. Thank you so much for this tutorial and keep up the great work, Cheers – Daniel.

    0
  56. 56

    You say: “Eclipse has a graphical layout designer that lets you build the interface by ‘dragging’ and ‘dropping’ controls around the screen”

    My question is: ¿Where i can get those ‘dragging’ and ‘dropping’ controls in eclipse helios?

    Thanx.

    0
  57. 57

    Awesome! Thanks a lot!

    0
  58. 58

    Thanks this article is helpful for my projects (peoplelink.in)

    0
  59. 59

    This was really helpful! Thank you for the great tips. I hope to see more.

    0
  60. 60

    for awhile i was lost, then i realize that i have to move on with my career… mobile app development? android or apple? I love opensource so it has to be android… i hope SM will put up an article about mobile app development.

    0
  61. 61

    Great tutorial. I’m learning android on the mac, and the program is running fine in the emulator, but not in the htc aria that I connected (and that is showing as a usb device in the finder). Any advice how I can get the program to load to the phone?

    0
  62. 62

    I did this tutorial, but I ended up with a different code than what’s hosted at github. Why would it be that? And my version doesn’t work properly. Has anything changed?

    1
  63. 63

    Fernando Agüero

    April 6, 2011 2:44 am

    Great article. I wish you write a second chapter.
    Thanks ;)

    0
  64. 64

    Thanks you are a good teacher.

    If i used your tutorial somewhere I will add your name and link back to this page.

    0
  65. 65

    Thanks For such a nice tutorial. And I will wait for the coming next tutorial. I have got the correct information.

    0
  66. 66

    Thank you man! Great article!!!

    0
  67. 67

    Could someone email me the end result because I have a couple of errors I cannot resolve.

    0
  68. 68

    Hey,

    i am getting the following error:

    View cannot be resolved to a type.

    Any advice???

    thanks

    2
  69. 69

    nice one for beginners

    0
  70. 70

    I dont get it :s I made something wrong, please could someone post the full code of the file # /src/com/example/brewclock/BrewClockActivity.java ??

    Thank You !

    0
  71. 71

    I’ve been thinking about playing around with Android App building and this article will be of great help to get me started. Thanks a lot.

    0
  72. 72

    Thnx aloot for ue great teturial,
    i want to debug my app on Galaxy tab , GT-P1000
    and my pc OS is windows

    0
  73. 73

    hello Mr. CHRIS BLUNT

    im a student mr. chris and i have to make a thesis and i ‘m going to develop a word game with the concept of text twist but i don’t know how to start with the use of android. Sir with your kind heart will pls help me and guide me in making my game? i want to learn sir please sir teach me how. thanks alot sir..

    0
  74. 74

    awesome! thank you.

    0
  75. 75

    Thanks a lot,Chris.
    nice post.

    0
  76. 76

    Hello Chris,

    Just came across your nice post.
    This may seem a bit advanced, but I am trying to use an Android tablet (MID 7″ tablet with 3G) with eclipse (instead of using the emulator, I want to use the actual device). However I am facing problem connecting the tablet to my desktop pc. I searched quite a lot on google but could not get the exact fix. I would be glad if you could help me in this regard.

    Thanks in advance.

    -1
  77. 77

    Hey
    I am following your Tutorial, because for me it seems to be the best broken down as you explain the different aspects and I believe that this is the way to learn effectively, I am having an issue with the following code from your list above:

    12 // Setup ClickListeners
    13 brewAddTime.setOnClickListener(this);
    14 brewDecreaseTime.setOnClickListener(this);
    15 startBrew.setOnClickListener(this);

    I am using Eclipse and attempting to type it out so as not to copy and paste and this way get myself accustomed to typing out the command and the above lines shows as an error and Eclipse corrects it as the following:

    brewAddTime.setOnClickListener((OnClickListener) this);
    brewDecreaseTime.setOnClickListener((OnClickListener) this);
    startBrew.setOnClickListener((OnClickListener) this);
    Iam wondering if I am doing something wrong or just missing something. Could you assist me on this.

    Regards

    Colin

    0
  78. 78

    It’s a nice post. mainly you have added the questions and concerns people come up with when they look and Android market and think of Android Game Development .

    0
  79. 79

    Hey mate… love your artickel.. but when try to build it.. i keep having problems with
    brewCountDownTimer = new CountDownTimer (brewTime * 60 * 1000, 1000);

    it says Cannot instantiate the type CountDoWnTimer???

    Any Ideas?? im pretty lost, i dont understand the problem

    0
  80. 80

    thank you so much for a very well explained tutorial.fortunately i didn’t get serious error except on warning on this part:(findViewById(R.id.etc)on but it’s gone when i modified the strings.xml based on your code.i just learned a lot from this tutorial.you’re a blessing..thank you so much!

    1
  81. 81

    ecliepes software works in windows xp 32bit?

    0
  82. 82

    thanks a lot for the info
    one query i already downloaded SDK and NDK and ADT how i connect with eclipse and
    i am student if u have more example project please send to my mail adds
    thanks in advance

    0
  83. 83

    it’s good..

    0
  84. 84

    It’s hard to find knowledgeable people about this subject, but you sound like you know what you’re talking about! Thanks

    0
  85. 85

    I followed the BrewClock Code and layout xml . on running it, i am getting an error message.
    Sorry,
    The application BrewClock
    (process com.example.brewclock) has stopped
    unexpectedly.Plese try again.
    and it is showing Force Close button.

    what might be the reason? I doublechecked bot code and xml, and they are ok. one major issue I encountered and changed by following eclipse quick fix was
    the event listening.

    // your plan of Setup ClickListeners
    brewAddTime.setOnClickListener(this);
    brewDecreaseTime.setOnClickListener(this);
    startBrew.setOnClickListener(this);

    eclipse changed to this

    brewAddTime.setOnClickListener((OnClickListener) this);
    brewDecreaseTime.setOnClickListener((OnClickListener) this);
    startBrew.setOnClickListener((OnClickListener) this);

    and finally ai got that error.

    0
  86. 86

    These features are useful in eclipse galileo version but not in eclipse because whenver i tried to make button in eclipse it tries to find some string source and when i write it like (“@string/abc”) , in place of abc ,,when i write anything which i like to show on user interface , then at that time this string keyword also shows,,i dont know how to remove that..????

    0
  87. 87

    not in eclipse classic,,sorry i forgot to mention..

    0
  88. 88

    Thank you for best Tutorial!!

    0
  89. 89

    Good tutorial for beginners like me. But I need more tuts

    0
  90. 90

    Hello, good tutorial but i got some problems, please could you make some changes on it if possible?
    Tks!!

    ADT 21.1.0 (February 2013)
    Dependencies (since december 2011):
    Java 1.6 or higher is required for ADT 21.1.0.
    Eclipse Helios (Version 3.6.2) or higher is required for ADT 21.1.0.

    Source: http://developer.android.com/tools/sdk/eclipse-adt.html

    0
  91. 91

    i am using linux mint os.eclipse support linux mint…

    1
  92. 92

    I am having trouble with the BrewClockActivity.java part. do you think you could either post/send the full code for it to help me figure it out

    0
  93. 93

    hey man , i just wanna ask a question . i’m a totally beginner in programming . i just copied the codes you wrote there and i’m having an error with this “setContentView(R.layout.main);” , help me please .
    anyway nice post . tnx in advance . ^_^

    1
  94. 94

    hi,
    I start to develop an Android application, but now, when I start Eclipse I find a new environment : ANDROID DEVELPER TOOLS with the name ADT, but with the same Eclipse icon . Please help me to comeback to my first Eclipse.
    thank you.

    0
  95. 95

    I totally agree with Tom. I’d love to see a lot more of these type of articles!

    4
  96. 96

    Upward compatibiliy is not a problem yet, but some things are deprecated and could not be available on future phones.

    -1
  97. 97

    I got the same uncomfortable feeling when I saw that too…

    1
  98. 98

    Thanks, Daniel!

    0
  99. 99

    I agree too :)

    0
  100. 100
  101. 101

    Sounds good – will keep an eye out for that! :)

    0
  102. 102

    Oh yeah, one thing I thought might be handy to mention in your article was this

    http://mikeyhogarth.wordpress.com/2010/09/29/android-beginner-top-tip-dont-run-from-xml-files/

    Absolutely will drive beginners NUTS – basically it’s possible to break the ADT to a point where you have to fix it manually, the above article describes symptoms and solution :)

    Cheers again for the article!

    0
  103. 103

    @Ganesh Not seen that before. A couple of things to try: When creating the new project (or importing the code into a project), be sure to choose “Android Project” in the New Project dialog.

    Also check the project is being built automatically by Eclipse (Project->Build Automatically is checked), sometimes cleaning the project files (Project->Clean…) helps.

    1
  104. 104

    Hi,

    Tried both of them but no change.

    What I observed was that even for a Hello World! example it is giving this error. Need to look at my Eclipse installation.

    Thanks for the help.

    Looking forward to further articles from you.

    Regards,

    Ganesh Bhat.

    0
  105. 105

    @Philip Thanks for the feedback, in future posts I’ll try to make changes to the source code easier to follow.

    I’ve started each block of code with a comment to show which file is being edited (# /src/com/example/brewclock….). You should not enter these comments into your code, they are just for reference. I do this as I’ve often found myself frustrated with tutorials and docs in the past that give you a chunk of code, but leave you guessing where to put it – especially when a project spans multiple files.

    Generally, you should not be overwriting any code, although some lines of code may change (such as “implements OnClickListener” which is added to the BrewClockActivity class declaration).

    I’ve used ellipses (…) to show where existing code should be left alone.

    It’s sometimes difficult to get a complete view of the code in a tutorial such as this, so I’d also recommend following the complete source code on github (http://github.com/cblunt/brewclock/tree/tutorial_part_1).

    1
  106. 106

    I’ve seen other tutorials where the current code is left black, and the newly entered code for step X is colored green, deletions might be in red…

    0
  107. 107

    Hi Cory,

    The “Hello World” message is contained in the strings.xml resource and read by the layout.xml file (I’ve copied the relevant lines below). You don’t need to write the message anywhere in your code.

    The BrewClockActivity that onCreate() method loads in and displays layout.xml (which contains the TextView). If you’re not seeing the message, check the code in your BrewClockActivity.java file matches below:

    <!-- strings.xml -->
    <string name="hello">Hello World, BrewClockActivity</string>
    
    <!-- layout.xml -->
    <TextView android:text="@string/hello" />
    
    // com/example/brewclock/BrewClockActivity.java
    package com.example.brewclock;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class BrewClockActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }
    

    Hope this helps, feel free to contact me if it still doesn’t work.

    -1
  108. 108

    Thanks Ash, appreciate your comments. I’m writing the next part of this tutorial at the moment, so keep an eye out on Smashing Mag :)

    1
  109. 109

    Hi Tam,

    I’m also using Ubuntu and ran Helios Eclipse for a while, although there were a few issues with the Android SDK.

    Although you’re running Helios, you can still download Eclipse 3.5 from eclipse.org, extract it to a separate folder (e.g. /opt/local/eclipse-3.5) and then run it separately from 3.6, for example by running /opt/local/eclipse-3.5/eclipse. You should then be able to follow the tutorial. Hope this helps!

    1
  110. 110
  111. 111

    Could you email me the end result of the brewclockactivity then?

    0
  112. 112

    Hi Christina,

    Once you have your project compiled well and running as expected in emulator, then you should find a ‘ .apk ‘ file in your work folder. All you have to do is transfer this file to phone. ( you could load it into the sdcard, or file transfer with bluetooth, send as an email attachment and download it to the mobile….., etc).
    Also make sure, you have checked the Unknown sources (Allow installing of non-market apps.), in settings > Application settings.

    1
  113. 113

    @Jacob K
    import android.os.CountDownTimer;
    OR
    Eclipse : Ctrl+shift+o

    0

Leave a Comment

Yay! You've decided to leave a comment. That's fantastic! Please keep in mind that comments are moderated and rel="nofollow" is in use. So, please do not use a spammy keyword or a domain as your name, or else it will be deleted. Let's have a personal and meaningful conversation instead. Thanks for dropping by!

↑ Back to top