I'm going to report back my confusions along the way as I learn Android development in 2010. My hope is the Author will be reminded what it is like to to start learning this stuff and that the feedback will help the next version.
So the Hello World program is usually presented as a chance to see and understand a minimal working program. But Chapter 2 misses the chance to explain how HelloWorld works and start explaining the types of Android Applications. This is
a big mistake because a novice reader wants to be assured that they can understand HelloWorld before they tackle the further chapters. Some people are reading to help decide if they want to do Android, or iPhone programming.
I could figure out:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
but I had to refer to
http://java.sun.com/docs/books/tutor.../override.html
to understand "@Override". I remembered "super" from past Java days.
However Listing 2.2 was a puzzle.
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
Even without having read Chapter 4, I think a brief description is in order. My immediate questions were:
1) What is "TextView" ? Is it a reserved word or a label chosen by the programmer ? ( I'm leaning towards reserved word seeing as listing 2.3 has the snippet new textView(this) )
2) What is going on here? android:text="@string/hello"
Is @string a reserved word ? Why the /
3) What is a LinearLayout anyhow?
So I run the program and it displays "Hello World HelloWorld", but I'm not clear how this output was generated. Must have come from:
setContentView(R.layout.main);
So two obvious questions:
setContentView is a method from what class ?
What is the type of R.layout.main ?
and where is R defined anyhow ?
My answers come slowly:
R is defined in the gen folder. Aha, The XML resources are used to generate Java source. Perhaps that is what the book calls "inflating", see Chapter 3.
So main is the int 0x7f030000
and therefore via
http://developer.android.com/referen.../Activity.html
public void setContentView (int layoutResID)
Set the activity content from a layout resource.
The resource will be inflated, adding all top-level views to the activity.
So what is so special about 0x7f030000? Is there a link between 0x7f030000 and the main.xml layout ?
Alright, so how did "Hello World HelloWorld" appear?
It looks like the TextView widget has text of android:text="@string/hello"
I guess "hello" is a label for some other resource perhaps defined in strings.xml ?
Hmm, If I double click on strings.xml, and then click on hello I see:
Name: Hello
Value: Hello World HelloWorld!
So I guess the answer is:
setContentView(R.layout.main); somehow links to main.xml
which must describe the layout of the application,
and has a TextView Widget that has text contained in a resource
which was defined to be "Hello World HelloWorld!"