Android Development 101 – Part 1:Hello World

This article will focus on developing a simple hello world program for android using Java. Google has recently released a “cute and fuzzy” programming environment for beginners to get into but I haven’t had the chance to try it, so we will be focusing on the Eclipse IDE here, which you should have set up in the last post.  When creating a text based project there are two very important items.  It will involve creating an android project, going through the necessary steps to complete both the (1) XML files and the (2) Java file and get this project ready for production and eventually deployment.  The requirements of this project are simple, know the basis of XML (for new comers if you don’t that’s OK too, you will learn) and know Java (very basic knowledge but you will learn as we go and we will modify a few parts of the java file today).

Start by firing up Eclipse and clicking File, New then Project. We are going to navigate to Android folder and select Android Project and click next. If you have updated your Android SDK correctly, your next screen will look like this. This screen requires we enter all data correctly including Project Name, Build Target, Application Name, Package Name, Create Activity, and Min SDK Version. These are important terms to know because they will control what keywords you can access and what your project looks like when you first create it. So to summarize:

  • Project Name: Will be the name you would like to use for your new project. In this instance we will use “HelloWorld” (I am a programmer and loath spaces you might choose to have them but it shouldn’t make a difference)
  • Build Target: This will specify what API you will be utilizing in your projects. It is key to choose wisely or you may lose out on a wider audience than anticipated. It is always best to think of what your program will use and see if a lower build will support those functions before cornering yourself with a higher API. For this program we are going to choose Android 1.5
  • Application Name: Will be the name you choose for your Application to be called. This can be changed later on and for right now we make that field equal Hello World.
  • Package name: The package name will consist of your company’s standard package name that you would use or in my case I use my entrepreneurial one. For this I am entering in com.gregrjacobs.helloworld but you can enter in anything you wish. Also, make sure it is in all lower case for good coding practices.
  • Create Activity: You will want to make sure that the box is checked before Create Activity because this option will do most of the work you just have to give it a name. NOTE: do not call this “Activity” as your project will cease to function and you have to go through the pain of making it all over again or manually correcting it. I am going to use HelloMain as my activity name, again use what you wish though.
  • Min SDK Version: This will have to be a number you enter denoting the platform you would like to target and if this application will show up in the marketplace of certain devices. If you look to the build target devices after platform tab on Android 1.5 you will see the number 3 and that will be our minimum SDK Version. Once everything is filled out press Finish.

This will create the core of our project ranging from the Android 1.5 JAR file to our Java and XML files. The most important things right now are the files in the src/com.gregjacobs.helloworld/ folder and the files in the res/ folder. First we will look at the main.xml which is located in res/layout/. This file will hold the layout of your application including textboxes, labels, dateTimePickers and the like. In the main.xml we will see that we are using a LinearLayout which will organize items into a single row or column. This can be handy sometimes (other times not so much) and for this example it will work, so leave it like this because we will be displaying text so it makes no difference. Next is the strings.xml file which will conveniently house
all of strings in the application. This is especially useful to keep the clutter out of your Java and main.xml file. We are actually going to change the string called “hello” from Hello World, HelloMain! to Hello Android, Hello World! And our string named app_name can stay how it is.

After doing this we are going to navigate to the package name we created and open HelloMain.java and this will contain the code to run the XML files we were just looking at. The first line will contain our package name that the java file is associated with. Next, in the picture above, we have imported the OS bundle and the activity function that will run our app. Later on when we explore adding textboxes and labels we will be importing widgets into our files and we will put the imports just under these two shown above. Next is our class that basically says when the application starts up, set the view of the screen to the layout labeled main. This is interesting in the sense that you can design many different layouts depending on the state you application is in. Of course with a hello world program we only need this one layout but the option is there for you to experiment with.

When we are done tweaking everything we wanted, we are going to build the application and run it on the emulator we created last post. To do this we go to the top of the eclipse screen and navigate to the button that looks like thisand press it. This will prompt you to say what type of project you would like to run. We want to run an Android Application and press ok.
The emulator should pop up onto your screen and load up. The first load time of your emulator will be atrocious but after that you should have no problems with load. When the emulator is done loading it may ask you to press Menu to unlock, do so by pressing menu just under the screen. You don’t have to navigate to load your project it will do it by itself it just has to load the rest of the software and you should end up with a result like this:

When working with tools like the eclipse IDE and AndroidSDK, making simple text based programs is a breeze. The next post coming to this series will get somewhat more difficult by developing an improved Hello World program. If anyone had any problems with this today make sure to leave a comment and I will try to get back to you before the next post. If you can’t wait to look ahead to designing an interface for your android application or looking into databases, these are the places to go; Data Storage and User Interface. Until next post, happy hacking.

Continue on to Part2: Graphical Elements

Comments

  1. n2o says:

    Now this is something relative to my intrests!

  2. Gottabethatguy says:

    Great post, well done, this is a welcome addition to the regular posting. A sort of how things are done/made/coded whatever type tutorial section would be great!

  3. Taylor Alexander says:

    Awesome! I’ve actually already messed with this stuff, but didn’t understand it all. A quick scan makes it look like this will be a great read when I have time.
    Thanks!
    -Taylor

  4. matt says:

    Hey, just wanted to say I’m digging the tutorials. The HAD soldering ones were great a while back as well. Definitely something I’d like to see more often. Helping to enable the readers to provide more content. Win win.

  5. amishx64 says:

    This is great! This what we were all hoping for with software-related hacks and such!

    – amishx64

  6. Jager says:

    Have to say that I am going to like this series. My wife is well-versed in Java programming (at least compared to me…), so I think I will 1-up her with Android Java Programming…

  7. CoreyMac says:

    Great article. I think this is a good fit for HAD, even though it’s not a hardcore-hardware-hack. I like the balance the site is trying to establish, and I think the quality and subject of this piece is a solid example of other tutorials to come. Speaking of tutorials, what do people here want to read about? Let’s get this new Greg guy to work! ;-)

  8. Jentulman says:

    This is a great idea, I hope this series continues in this vein.
    As a full-time PHP guy I’ve stayed away from java, but being an iphone to android switcher I’d love to start digging into a bit of app dev, and it’s a great excuse to learn a new language.
    Plus android phones seem to be a growing choice for the ‘brains’ of projects, and HAD seems a great place for maybe heading in this direction in some of the tutorials further down the line.

    Keep up the good work Greg, it’s much appreciated.

  9. biozz says:

    great … im past all this hello world stuff but for thoes who are not these are very well structured and i hope this slowly moves up to super advanced programming for the android platform
    (yes i said “super” >_> )

  10. Mike says:

    This looks like a lot of fun. I’d be really interested in picking up a low-cost portable android to play with. Does anyone have suggestions for where to get a inexpensive android platform (without out a cellphone contract please).

    Thanks!

    • @Mike – There are a couple phones out there that are especially cheap…. HTC Magic (will not surpass Android OS 2.1 maybe 2.2) or the HTC Dream (This will not pass Android OS 1.5 due to hardware issues… although you could “hack” it to go higher :) ) best places to buy are on ebay right now and you could pick one up for $100 – $200 maybe lower if you look elsewhere. These phone would allow you to target all Fragments of Android plus allow you a cheap test solution. That is if you don’t like the built in emulator :)

  11. MS3FGX says:

    This series is exceptionally well done so far, and I particularly like that the author is willing to help individual users with problems.

    While it isn’t the traditional HaD style, I think it absolutely fits with the sense of discovery and community that we already have here and is an excellent addition to the site.

    After the embarrassing train-wreck of an Android article from that “other guy”, this is a welcome addition and gives me considerable hope for the future of this site and the community it is built on.

  12. Inetd.conf says:

    this whole article feels like a quick re-hash of the android.com hello world. HAD lets get back to the hacking and not this A for apple, B for ball programming intro which can be found on 20 other sites if one bothers to use google.

  13. Dan says:

    Absolutely love these posts! Literally the day before the andriod post series I was struggling through trying to write a few of my own apps. Keep it up HaD!!!

  14. biozz says:

    @Inetd.conf
    ITS A SERIES …to learn to read you need to learn A for Apple and for programming you need to learn hello world
    just because you know the basics does not mean everyone else does

  15. Nick says:

    Love these tutorials just like everyone else. Please continue them, they are very helpful to those of us who are new to java and android developing :)

  16. jeditalian says:

    i remember doing this in java class. i really wish i hadn’t fried my brain and got a D on the final, cuz i would be so rich selling kickass apps if i would have only kept working with java

  17. samaral says:

    Cheers to the writer for the wonderful post. Looking forward to more of this too!

  18. sylware says:

    I speak for myself:java is a very bad move. If I code anything for android, that will be to rewrite it in C/ASM.
    But since, android is the most opened mobile OS with broad adoption, I will promote it by using it, till we have better. Still no proper IMAP app, XMPP app…

  19. Really cool HowTo! Can’t waiting for the next part!

  20. mess_maker says:

    It’s great to see good comments on here. :)

    Great article, keep it up.

  21. Eric says:

    As a programmer this article isn’t really useful but I find it really good that HaD has beginner code articles, just as it should have some beginner hardware articles (I’m trying to learn how to build electronics, so I can see how hardware guys could benefit from such article).

    Thanks Greg!

  22. JMLB says:

    is HaD really the place for this?

  23. Woofpickle says:

    As the proud owner of an eken m0001 and htc hero, and a definate need for a skeinforge/repg port, this thread is very relivant to my interests.

    Keep ‘em comin’!

  24. redleader36 says:

    @Mike – today the Archos 5 is on sale at Newegg for $149 free shipping. I beleive that runs android 1.6.
    link: http://www.newegg.com/Product/Product.aspx?Item=N82E16855501050

  25. James says:

    Great first part, can’t wait till second part. Not done any programming since BBC Basic days.
    Now if someone wrote the classic ‘Candy-Floss’ game on Android platform summer 2010 would be complete.

  26. I got Eclipse and the SDK installed and was able to launch my Hello World program on my Nexus One over the ADB successfully. Thanks!

  27. DavioP says:

    I keep getting an error when I build the HelloWorld app to the effect that there is an “Error in an XML file: aborting build”. It appears to have to do with the strings.out.xml file that is generated at build. Any ideas that might help me work through this?

  28. DavioP says:

    I’m a little slow this early in the morning. I discovered what I was doing wrong. I’m looking forward to moving ahead.

  29. @DavioP – Just out of curiosity, what was your issue with the build exactly? Just in case anyone else comes across that issue we will have the fix in the comments :)

  30. Jones says:

    Really good to see a series of articles like this, even if it is pretty much a rehash of info on the android site.
    It would be really good to see articles about hacking with (j)ruby/(j)(p)ython on android.
    That would be some real hacking and would really help a lot of people break into programming cross platform.

  31. pascal says:

    You should mention, that getting ADB to run is quite important.

    On my system, the emulator just as abysmal performance, downloading the app to my phone and even debugging works like a charm though. (While the emulator frequently crashes)

    (who misread the title for “Arduino Development: Hello World”, too? Glad I didn’t have to see that comment thread)

  32. E_man says:

    I’m having an issue. Whenever I go to layout and open main.xml, eclipse crashes. Whenever I go to values, and open strings.xml, I get java.lang.NullPointerException errors, and it’s blank. Adding strings gives me more of the same error.

    What’s going on?

  33. @E_man – Can you walk me through the steps you took before getting to this issue (creating your project), what system are you using and and some screenshots to my email please of your project and its issues?

  34. John says:

    Not very “Newb” friendly… too many missing steps and explanations. And after several hours of getting the SDk and eclipse to work, eclipse keeps crashing. What exactly do you do to “look” at the main.xml?

  35. Simbo says:

    @E_man i’m having the exact same problem. i’m using win 7 x64.

    @Greg, emailed screens to you!

    any help would be awesome

  36. Doug says:

    @Simbo & co.
    re: java.lang.NullPointerException when trying to edit strings.xml

    This is a bug in the latest build:
    https://bugs.eclipse.org/bugs/show_bug.cgi?id=318108

    As a workaround, go to the Window / Preferences / XML / XML Files / Editor page and turn off the “Use inferred grammar…” setting.

  37. Simbo says:

    @Doug: thanks, i was able to load string.xml but once it was open i could not edit anything as it froze yet again.

    @Greg: i’m downloading the 32bit version to try as asked in the email.

  38. Simbo says:

    32-bit version seems to work.

    its painfully slow though!

  39. Simbo says:

    New error!

    [2010-07-19 22:43:06 - helloworld] Error in an XML file: aborting build.

    any ideas anybody?

  40. Simbo says:

    found the solution on http://www.anddev.org/mainxml_error-t7506.html:

    hi there,
    had the same problem and now i found the solution!

    Just make sure when you run the application, that you are editing an .java file
    and not an .xml file!

    and you have to delete “main.out.xml”

  41. John says:

    I cant imagine that Android has amassed 70,00 Apps using this program. Has anyone found a tutorial on setting up eclipse properly? I’m using W7 64 bit.

  42. John says:

    Here’s a fix I used the second choice:
    The Eclipse launcher for Windows has a problem with the latest
    Oracle/Sun Java VM update (java version “1.6.0_21″). You have two
    choices to workaround this:

    1. switch back to Update 20
    2. add the following line after “-vmargs” to your eclipse.ini:
    -XX:MaxPermSize=256m

    Hope this works for others!

  43. MDSanta says:

    took a few tries, kept hitting error after error. But thanks to the comments I managed to fix them all. The loading time was in fact loooooong. About 7 minutes for me. I had to keep re running the emulator as Eclipse kept giving up lol. Eventually it ran and now I am a happy camper. Thanks dude.

  44. drew says:

    Nice little tutorial, got me up and running quickly. I’ve programmed in Java before, but I knew programming for Android wouldn’t be as easy as “javac *.java”

    One thing though. I tried copying HelloWorld.apk from the bin/ directory over to my Moto Droid and when I tried to install it, it says that this app needs to modify SD card contents or read Phone call info. Of course, this app shouldn’t need either of those. How can that be changed? Other than that, it runs just fine.

  45. Nick Hodges says:

    Hey —

    Experienced Delphi developer, but total noob to Eclipse, Java, and Android development.

    I can follow along and seemingly get everything to work, but when the emulator appears, it is blank for a second, and then it merely puts

    A N D R O I D

    in the middle of the screen and doesn’t respond in any way.

    Am I missing something? Am I supposed to change the code at all?

    • Howdy Nick,

      How long does it stay at that A N D R O I D screen? some computers when starting it up have taken up to 2 – 3 mins (my crappy xp server :( )? Any solutions we find I will ask that you post to the comments so that the community can build on their knowledge of Android dev :)

  46. Peter says:
  47. Nick Hodges says:

    Greg —

    Thanks for the response.

    Okay, first, I double checked that my Android SDK was totally up to date, and apparently it wasn’t. So I ran the updates.

    Then, I ran my app again.

    After a few minutes, it then goes to a graphical “Android” that “glimmers”.

    Then, after about 6 or 7 more minutes, I got a crash error in the Alarm Clock, and then my app finally did appear.

    I like getting a crash error in an app in an emulator. Fun! ;-)

    Anyway, so far so good — will I have to wait this long for the emulator every time? Yikes.

    Th

  48. Nick Hodges says:

    One more question — Should I leave the emulator running between executions, or do I need to shut it down each time?

  49. Big P says:

    Done with lesson 2, on to lesson 3!

  50. BobBob says:

    The suggestions about the fixed for the ide crashing worked for me and I have been searching for this for days thank you all.

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 92,407 other followers