Get $500+ of the best After Effects files, video templates and music for only $20!
Introduction to Android Development
basix

Introduction to Android Development

Tutorial Details
  • Technology: Android SDK
    Difficulty: Beginner
    Estimated Completion Time: 45 Minutes

Mobiletuts+ will be covering all major mobile platforms – iPhone, Windows, Android and Blackberry. Today we’ll be taking a look at Android development: explaining why people have choosen to work with Android and providing an overview of the Eclipse IDE and its Java, DDMS and debugging perspectives. Finally, you’ll learn how to get started making your first Android app!


Android 101 Tutorials:


What is Android?

Android is an open source mobile operating system that combines and builds upon parts of many different open source projects. What does this mean to you as a developer? You have access to the source code of the platform that is running on the phone. This can help you better understand how interface controls and the various other pieces work. If you happen to find a bug, you can also submit a patch for the issue, though this is a more advanced practice. Google has also pulled together a large group of companies (called the Open Handset Alliance) that both contribute to and use the Android OS in their hardware devices. This means that there is industry-wide support for Google’s OS, promising wide adoption across well-known vendors.

Why Android?

There are many advantages to developing for the Android platform:

  • Zero startup costs to begin development. The development tools for the platform are free to download, and Google only charges a small fee to distribute applications on the Android Market.
  • Freedom to innovate. The Android OS is an open-source platform based on the Linux kernel and multiple open-source libraries. In addition to building applications to run on Android devices, developers are free to contribute to or extend the platform as well.
  • Freedom to collaborate. Android developers are not required to sign an NDA and are encouraged to collaborate and share source code with each other. According to a survey by Black Duck Software, the number of open source mobile apps and libraries grew at a rate of 168% from 2008 to 2009, faster on Android than any other platform. This means more code that you can reuse in your own projects to bring them to market much faster.
  • Open distribution model. Very few restrictions are placed on the content or functionality allowed in Google’s Android Market, and developers are free to distribute their applications through other distribution channels as well.
  • Multi-platform support. There are a wide variety of hardware devices powered by the Android OS, including many different phones and tablet computers. Development for the platform can occur on Windows, Mac OS or Linux.
  • Multi-carrier support. A large number of telecom carriers currently offer Android powered phones.

Prerequisites before continuing with this article include:

The Eclipse IDE

Eclipse is a complex, multi-language, and extensible Integrated Development Environment (IDE). The learning curve can be steep, but the power of the environment can greatly increase your efficiency.

After opening Eclipse for the first time, select a workspace to save your project within. You will see an introduction screen with multiple icons. Select the “go to workbench” option, and you will be presented with the default project screen.

Assuming you have already installed the Eclipse ADT plugin, you will need to configure Eclipse for Android development by manually setting the filepath for the Android SDK. To do this, select Eclipse > Preferences from the main tool bar, and then select Android from the dialogue box that appears. Update the “SDK Location” option to point to the directory where you installed the SDK. You should now have the IDE configured for Android development.

It is important to note that Eclipse uses something called “perspectives” to group commonly used tasks. Switching perspectives will switch out parts of the menu and toolbars, and will show and hide views related to them. Perspectives can be opened by clicking on the Open Perspective button or by choosing Window > Open Perspective. Some perspectives that you will use frequently include Java, Debugging and DDMS.

The Java Perspective

The Java perspective is the default perspective in Eclipse, and it is where you will probably spend most of your time.

Java Eclipse Android Development

Among the most important views in this perspective is the Package Explorer view, by default located on the left hand column of the workbench. This view is an overview of your entire project. It also shows the states of individual files with regard to compile issues, version control, etc.

Another important view in the Java perspective is the Problems view, by default located in the bottom center panel of the workbench. This is where you will find compile warnings and errors listed. You can double-click an item to be taken directly to the error in the Java or XML file.

The DDMS Perspective

DDMS is short for Dalvik Debug Monitor Server, which communicates with the low-level services of a device or emulator. Switch to the DDMS perspective now by selecting Window > Open Perspective > DDMS.

DDMS Eclipse Android Development

The Devices view, located in the left column of the workbench, is where you will see any Android devices available to your computer. This includes both phones attached to your machine and running emulators. Under each device, you will see all the running processes. There are toolbar buttons on the view for launching the debugger on a process, getting information about heaps and threads, stopping processes, and taking screenshots.

The Emulator Control view, also in the left column, lets you do the following:

  • Set the status of the voice connection.
  • Set the status, speed and latency of the data connection.
  • Simulate an incoming call or SMS from a supplied phone number.
  • Provide a simulated set of points for the GPS via a latitude/longitude point, or GPX/KML file.
  • Using the File Explorer view, accessible as a tab at the top-right of the center column, you can browse the file system of a device. For an emulator or a rooted phone, you will have access to the private directories /data and /system. For non-rooted phones, you will only have access to /sdcard.

    The Debugging Perspective

    The debugging perspective will provide in-depth information about your applications. Switch to the debugging perspective now by selecting Window > Open Perspective > Debug.

    Eclipse Debugging Android Development

    The Debug view will show you the running apps being analyzed, and, when stopped on a breakpoint or exception, the call stack of the application as well. The Variables view displays the contents of any local variables at the current breakpoint.

    The LogCat view in the lower right hand corner displays all logging output using the android.util.Log class. You can filter based on tags, or different log levels such as debug, information, error, etc.

    Your First Application

    To begin creating an Android application, switch back to the Java perspective and select File > Menu > Android Project. Doing so will launch the application creation wizard, and you will be prompted to enter meta-information about your project in three categories: Contents, Build Target, and Properties.

    Android Application Wizard

    Name the application “DemoApp” and leave the Contents section with all the default values.

    The Build Target section defines the version of the SDK that our demo app will be compiled against. For this tutorial, choose API level 4 (Android 1.6) because it will run on a wide range of hardware and the API will allow us to handle different screen resolutions.

    Next is the Properties section, which provides the wizard with more information about what classes to generate and what they should be named. The Application Name setting will be displayed under the application icon, as well as the application’s title bar when launched. The Package name provides the base Java namespace for your generated classes. In order to create a default activity, make sure Create Activity is checked and provide an activity name. The last option is the Min SDK version. This value determines what version of Android needs to be on a phone in order for this application to be installable. This is generally set to the same API level that you chose under Build Target.

    Once you enter all this information and click Finish, you will have a basic “Hello World” application that is almost ready to run on a phone or an emulator. Before we setup an emulator and run the application, take a few minutes to examine the standard template content generated:

    Default project content view

    The AndroidManifest.xml file

    The AndroidManifest.xml file provides metadata about your application that the Android OS will need to run the app properly. The name of the application, used for both the app icon and the activity titlebar, and the app icon are defined under Application Attributes. You will notice that the Name field doesn’t actually contain the name text, but “@string/app_name” instead. This is a string reference and can be used anytime a string is expected. The actual string text is then defined in one of the XML files found under the res/values folder. The app creation wizard generated a file there called strings.xml.

    The Application Nodes section is where all the activities are defined for the application. Our app’s single activity is called MainActivity and listed here.

    The /res folder

    The res folder is where most application resources are stored. The main content categories include drawables, layouts, and values.

    Drawables are generally bitmaps images in the form of .PNGs. Drawables can also be nine-patch images, which are .PNGs with special data in the image that help Android do a better job when stretching the image. Nine-patch images can be created with the nine-patch tools in the SDK, or with an image creation tool like Photoshop.

    Layouts are where you define your screens. To view the XML for the layout on screen, click the main.xml tab.

    Values are where you define (in XML) your globally used colors, dimensions, strings and styles. The strings.xml file allows you to add and edit values for your project.

    The /gen folder

    This is where code is generated for all the resources defined in your res folder. This is how you can access layouts and controls defined within your code.

    The /src folder

    The src folder contains all of your custom source code, grouped into packages. Packages are simply there to help categorize your source code into logical (and manageable) groups.

    The /assets folder

    The assets folder is a place to store miscellaneous files you need to access in your code as raw data. All files in the res folder have methods to load the specific types, whereas the only way to load something from assets is to programmatically open it as a file.

    Creating an Android Virtual Device

    Virtual devices make it possible to run and test your code without owning an actual Android phone. Since there are several different version of the OS you can target, you will eventually need to create multiple versions of virtual devices, but for now, we’ll create one using API level 4 (1.6). You can do this via the AVD Manager. From the main toolbar, select Window > Android SDK and AVD Manager.

    Android Development Virtual device creation

    Once you’ve opened the manager and are viewing your list of virtual devices, click the “New” button to create your virtual device.

    I generally name my virtual devices using the OS version number along with the preset resolution that I choose, so in this case, 1.6-hvga. It’s good to also create an SD Card for the emulator which I usually set to 16MB, unless I know I will need more space. Click the Create AVD button, and you will see your device listed.

    Android Development Configuring virtual device

    Go ahead and start the virtual device by selecting it and clicking the “Start” button.

    Running & Debugging Your First App

    Eclipse, along with the Android Developer Tools, offers a great environment for debugging applications. For debugging, you’ll use both the Debugging and the DDMS perspectives. The debugging perspective will be used for stepping through code, viewing values of variables, and setting breakpoints. The DDMS perspective will be used to control the emulator, view threads, and view memory allocation.

    Since this is our first time running the application, we need to create something called a run configuration. Run configurations are the settings that Eclipse will use to run (or debug) your application. Each application can have multiple configurations. One might be set up to always deploy and run on an attached phone, and another could be setup to only run in a specific emulator instance. At this point, disconnect your phone if you happened to have it attached to your machine so you can see the app run on the emulator first.

    To create the run configuration, select DemoApp in the Package Explorer, then choose Run > Run from the main menu. In the next dialog, choose Android Application and click OK. The emulator that we created earlier should launch. When the emulator first starts up, it may appear with the lock screen; just click menu to be taken to your new app. You should now see the text “Hello World” on screen!

    Android Hello World Screen

    Our next step will be to set a breakpoint. Open the MainActivity.java file by double-clicking it in the Package Explorer. It is located under /src > com.demo.demoapp. Next, on the line that contains:

    "super.onCreate(savedInstanceState)"

    double-click in the gray column to the left of the line (where you see the blue circle in the screenshot below). If you were successful, there should now be a blue circle indicating the breakpoint.

    Setting a breakpoint in Eclipse for Android

    Now switch to the debugging perspective by selecting Window > Open Perspective > Debug . To debug the application, select Run > Debug.

    In the Debug view, you should see a list of items under DalvikVM/Thread. This is the call stack since we are now stopped at the breakpoint we set earlier. The Variables view will show all local variables at the current breakpoint. You can expand the item “this” to see all the values of our MainActivity instance.

    Debugging information

    Finally, LogCat will display all logging information coming from the emulator.

    To continue running the app, you can use the toolbar on the Debug view (as seen in the screenshot above), or choose the same actions from the run menu. Choose Run > Resume to let the app continue running.

    Conclusion

    This tutorial took you through the various parts of Eclipse and ADT that you need to be familiar with to begin Android development. Although we didn’t cover the details of the application source code, it is important to start with a strong understanding of the tools you will use every day during development. In coming articles, we will continue to dig deeper into writing progressively complex applications and creating a compelling UI.


Related Tutorials:


Tags: Basix
Add Comment

Discussion 65 Comments

  1. Ivan says:

    Thanks for sharing this article! It's a great post to start coding for what's going to be the most dominant mobile OS!

  2. Karthik says:

    Thanks,

    I've been looking for this.

  3. This is really a nice article on Andriod.

    Mobile tuts is really inspiring me to be a mobile developer as well ;)

    Keep up the good work !

  4. Clément Roy says:

    Ah what a nice article! I have been playing with Android for several months now, and I have to say it is quite powerful.

    Can't wait for more advanced tut!

  5. Andy Jaeger says:

    Thanks for this article. I'm just getting started with Android development. I look forward to a bunch of great content on this new Mobile extension of the tuts plus network!

  6. Julian says:

    I've just started with playing with the Android SDK yesterday.

    Anyone being really interested in developing an App for Android should definitely check out Google's tutorials as well: http://developer.android.com/resources/tutorials/

    I'm looking forward to seeing more Android tutorials here :)

    • 53x7u5 says:

      Android-tutorials are very exclusive content. I'll worried too about it at mobile.tutsplus, but it seems to be a lot content about iPhone and WinPhone there.

  7. This looks quite a bit easier than dev' for the iPhone but I'm afraid that's where my focus is going. :P

    • Jared says:

      You should really do both. Most hardcore devs and anyone that owns an Android, including myself, will agree that it may be too late to start learning how to develop for iPhone as the distant future looks very bleak for it. "General" users are beginning to realise that the iPhone is considerably behind in mobile device technology compared to Android and that's why the Android sales are phenomenal and are literally doubling themselves every month.

      It's idiotic to say the iPhone is dead, though that's all you seem to see on Android sites and developer's forums, but it's also very silly to look at the current trends and blindly say the iPhone will remain strong in the market. On the topic of markets, Android is already being used in on-board car systems, entire notebooks, etc. in addition to over 30 different mobile devices.

      With this in mind I would STRONGLY advise you to learn how to develop for Android devices. I personally wouldn't get too involved with the iPhone until it's future is more clear.

      • James says:

        You are looking at the situation from an engineers perspective, and thus are completely biased. "Normal" people, 99% of the population, don't care whatsoever about the things Android has over the iPhone. The things that matter to them are integration, and ease of use. The iPhone absolutely dominates Android on those fronts. Google does not have, and will likely not have anything even close to the iTunes ecosystem anytime soon. As well, the iPhone accounts for 99% of mobile app sales. While you may be a good developer, if you don't understand the importance of this fact, you are a terrible business person.

        Keep in mind that the iPhone is only available on 1 carrier currently in the US. Once they get an iPhone on Verizon, Android is going to take a beating, because a good majority of the people currently buying Android phones are only doing so because the iPhone is not available to them.

        So if you want to develop for Android, then go ahead. But don't for once second think that Android is the future, because it's not. iPhone OS will be the dominant mobile platform for a long time to come. Apple has shown that it is vastly superior to any other company and making and marketing consumer products. If you think Google can surpass Apple in the consumer hardware space, you are sorely mistaken.

    • Mitch says:

      Apple is a marketing company they will always be able to sell their garbage and make people believe they need it. Android sells itself. That is the difference between the two.

    • evanism says:

      per comments above, esp benji, I am becoming frustrated with IDEs. I use PHPed and Eclipse on other jobs. I loved the article, and despite everything, picked up a snippet.

  8. Aafrin says:

    This is a wonderful tutorial. very detailed explanation… gonna write my 1st android app very soon…

  9. Chris says:

    Great!! more please!! :)

  10. xRommelx says:

    really nice tut

    i will learn this

  11. Veeti says:

    It would have been nice to see something more complicated – like a clone of the "iPhone fortune cookie" tutorial you guys have.

  12. Ed says:

    Fantastico

    Thanks!

  13. Chris L says:

    This is great! This has got me excited to get into mobile development :)

  14. AGDM says:

    Can I use Netbeans in place of Eclipse? I'm already using Netbeans and would like to try this out. :)

    • 53x7u5 says:

      Of course you can! Its looks a same to work with Android SDK in Eclipse or NetBeans. But i haven`t read any android+netbeans tuts. Because of a Eclipse popularity.

    • Gyuri Grell says:
      Author

      Searching for “netbeans android” shows plenty of results on how to set it up. I’ve heard of a few people using it.

  15. Michele says:

    After reading this article I kinda stopped thinking about why there are so many more quality apps for iPhone.. I mean at least you have an UI designer there.. I hope they GO and find a better way.. (that was an extremely nerdy joke for a very selected few =))

  16. charlie says:

    Although the iphone has a ui developer just about any successful program will have its own unique ui and will be coded for it ui

  17. Thurein says:

    Really great… I have been looking for it. I m going to develop Android app. Thanks

  18. David says:

    …where was this 4 months ago!! :)

    Awesome beginning, thank you!!

  19. Justin Goren says:

    I highly recommend not bothering with android development. Heres some reasons why:

    1. The adnroid market is filled with horrible apps and spam that will colver your app up almost entirely.

    2. People have reported that while their app has sold a ton on the iphone, it mabye only sold 4-5 on android

    3. Personally, i find Xcode a lot easier to navigate than Eclipse

    4. MUCH MUCH MUCH more spam in android market

  20. Jesse says:

    Love it. Thanks. I'm looking forward to more.

  21. Serg says:

    Super glad to see this. More please. =]

  22. Drazen Mokic says:

    Java… why the fu** they have choosen JAVA ? An old, bloated language… i hate them for that reason.

  23. Curtney says:

    Great introduction to android development. Keep up the good work! Looking forward to more tutorials.

    Thanks.

  24. xDee says:

    It's kind of sad how people are thinking about the Android App Store, do you have an Android phone? Sure there aren't any killer 3D apps but there are a lot of good and successful apps… The Apple App Store wasn't filled with high quality apps from the beginning either so I think the Android App Store will do just fine in the future. i mean come on people, sure it's easier to have a super-duper UI development environment but hey you can design your own UI and every app has the ability to have an own cool new look. And if you like it the easy way you can still use Titanium to create native-looking apps. I love Titanium and it would be really interesting to have some tutorials about it! So in my opinion developing for Android is one of the best things I started doing this year. And: one of the things I like the most about it: You don't need to buy a freaking Mac computer and pay for a license to develop apps…

    Hope to see more tutorials about this. Thanks

  25. Basu says:

    Thanks for sharing this article!

  26. 53x7u5 says:

    It will be pretty nice if someone will wrote an article "How and where to sell Android-applications" (if Google Checkout doesn't work with your country).

    Its a real problem for most developers in East Europe and Asia and South America.

  27. John says:

    Nice ! i like this …i hope i will try it

  28. Darren says:

    I think it's a sign if I can't install the software to create the apps, I will probably have more trouble creating the apps themselves. lol

  29. wanluqman says:

    Mr Gyuri,

    Thanks for the intro. I am going to download eclipse.

    You are right steep learning curve doing the ide, tried once & failed.

    Gurus needed.

  30. Super excited that mobile tuts is doing Android development. Hopefully even more developers will migrate to Android.

  31. A great intro to Android development. Although I see a lot of Android haters in the replies. I agree that Google should really do something about the growing number of junk apps in its market. On the other hand this is one of the downsides of open source environment. Any tool is only as good as the person who implements it in the correct way. I have started a forum for mobile app developers.

    Everyone is welcome to join the forum at [http://wedesignapps.com/forum]

    Although the primary focus will be on Android OS this forum addresses the collaborative efforts of developers working alongside User Interface designers for bringing up quality apps.

  32. prasanthtm says:

    nice tutorial , i tried my self after reading article , its working fine,
    thanks

  33. Android is awesome. But you could have some Java ME tutorials as well. It still has the largest share of the market (at least in Europe, where people don't trust iPhones)

  34. Desu says:

    I'm beginner. Thanks for sharing. Nice post.

    *But hey, I wonder what OS you are use for develop? I like the window bar theme :)

  35. baroquedub says:

    Thanks for getting us newbies started :)

    I ran into problems after initially installing Eclipse, the Android SDK and the ADT plugin. On Eclipse Helios build: 20100617-1415 I found that the first project initialised with an error claiming that the project is missing the required source folder: 'gen':

    see http://baroquedub.co.uk/private/eclipse-android-e

    although the gen folder does actually appear in the Package explorer!

    see http://baroquedub.co.uk/private/eclipse-android-e

    For those with the same problem, the solution is to go into the project properties > Java Build Path and to click on browse to select the folder again.

    see http://baroquedub.co.uk/private/eclipse-android-e

    Thankfully you only have to do this once as all subsequent projects initialise fine.

  36. Hi,

    Thanks for this useful tutorial – I have referred to it several times when writing my own Android (game) tutorial: http://www.quesucede.com/page/show/id/conway_game….

  37. Maor says:

    As a beginner in the Android development world, I would like to thank the writer of this article and of course MobileTuts! Thanks for sharing!

  38. Ciarán says:

    Thanks for the article.

    I am looking froward to developing applications for Android.

  39. polux says:

    excellent…

  40. It Village says:

    looking for such tutorial. happy to read it.

  41. Neekey says:

    Helpful! Thanks a lot!!

  42. The one thing I hate about the android market is the ton of spam you get from it.

  43. honour chick says:

    very detailed explanation, thanks for sharing this.

  44. prince says:

    the most I like from android is “Freedom innovate”. and many choice for free application.

  45. Moutaz Alazab says:

    thanks for the informative information. actually I had question how to get a list of the API calls that has been used by Malware applications, and get another list of the API calls that have been used by benign programs. Result, trying to build a detection engine based on the calls of API calls in android.
    Is there any tools that support extract the APIs

  46. Sarmad says:

    Hi
    I am setting up my environment for android development for the first time. And having a trouble now.
    When I created the virtual device it was created successfully but when I try to start it whether from inside the eclipse or outside it it gives me an error,
    “invalid command-line parameter: Files.
    Hint: use ‘@foo’ to launch a virtual device named ‘foo’.
    please use -help for more information”

    I am stuck at the very first step :-(
    Please help me out
    Thanks

  47. Hello !

    What’s the version of your Eclipse?
    Thx !

  48. Vishal says:

    very essential tuts for entry level learner….. trying my hands at android… this website is really helping me a lot…… i just love all the tutplus blogs…..

  49. Useful information. Lucky me I found your site accidentally, and I’m stunned why this coincidence did not took place earlier! I bookmarked it.

  50. I wish I knew how to program Android apps when it first launched. Now all my ideas are taken. This was a helpful article. Thanks!

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.