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