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

80 thoughts on “Android Development 101 – Part 1:Hello World

  1. 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!

  2. 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

  3. 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.

  4. 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…

  5. 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! ;-)

  6. 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.

  7. 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” >_> )

  8. 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!

    1. @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 :)

  9. 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.

  10. 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.

  11. @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

  12. 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

  13. 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…

  14. 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!

  15. 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.

  16. 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?

  17. 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.

  18. 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)

  19. 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?

  20. 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?

  21. @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.

  22. 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!

  23. 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.

  24. 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.

  25. 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?

    1. 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 :)

  26. 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

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.