Friday, May 27, 2011

Beginning Android Tutorial #1...Seriously, The BASIC Hello World Program (Part 1 of 3)

  
So we all have to start somewhere (usually the beginning), and I am no different so I am going to do the same as what is seen on all over the web in terms of a first program. That is the typical 'Hello World Program', but what I think I am going to do differently from what I have seen on some sites is to continue a program for three parts or so to expand upon the initial program. So this will be part 1 of 3 and I will progress in difficulty as I progress within the world of Android.

     I am also assuming (never good but whatever) that any readers will already have some familiarity within Eclipse (I am using Helios) and navigating within it to access the appropriate files (i.e. source, assets, resources, manifest, layout, drawable, etc.) as well as creating new Android projects.

  • Part 1/3- Basic 'Hello World Program' with TextView
  • Part 2/3- Addition of EditText
  • Part 3/3- Addition of a Button with an onClick listener

     Also if you have decided to use anything within my blog please bare with me as I find the best ways of making this information available to you, and as usual suggestion and comments are always welcomed...moderated, but welcomed so long as they are respectful and constructive.

Build:  2.1 Update-1
Project Name: BasicHelloWorld
Activity Name:  HelloWorldActivity
Min. SDK Version:  7

      Upon completing the initial set-up of the project the program is essentially done for you with respect to the initial goal.  Android has created a main.xml file that contains all of the elements we need.

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" >
<TextView
    android:id="@+id/display_text"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"/>
</LinearLayout>

The reason that I gave an id to the TextView this early is because I will be using this widget in Part 3 and it will need to have an id by then...so might as well put it in early.

The only thing I am going to edit at this point is within the res/values/strings.xml file. I am changing the hello string to read 'Hello World, welcome to Program #1!'.

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, welcome to Program #1!</string>
    <string name="app_name">HelloWorld</string>
</resources>


Get the source code for this project here:  BasicHelloWorld.zip

Part 2 and 3 will be up this weekend and much, much more to come!

No comments: