Chapter 3 (Designing the User Interface) talks about designing the User Interface using either the procedural way using Java or declarative way using xml. An activity represents a user interface screen. Inside an activity, Android calls the onCreate() method to initialize the activity within which you will call setContentView() to fill-in the contents of activity screen with an Android view widget. For example, setContentView(R.layout.main) is a resource identifier that refers to main.xml file in the res/layout directory of your Android project. Here, main.xml is the user interface file and any changes to that file will have a corresponding change in the R file. At runtime, Android parses and instantiates or inflates the resources defined there line by line and create a java class for each of the element and a setter for each element i.e. xml will be inflated into Java.
The chapter further drills into the structure of the user interface xml file. The file contains a reference to Linear layout which is a container for the child objects (TextView, Button) and a way to position them on the screen within the rectangle of the parent object. Linear layout is the most common layout and it arranges its children in a single column or row.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- ... -->
</LinearLayout>
Common view properties are fill_parent (or match_parent) - takes the whole space of the parent and wrap_content. Android looks at the user interface xml file starting from parent and then moving onto childrens one by one.
This chapter further talks about event handling mechanism to create a new activity in the form of an alert upon click of a button. Inside the onCreate() method of an activity, one can call the findViewById() to look up an Android view given its resource id and a call to setOnClickListener() to let Android know which object to tickle when user clicks the view.
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
One option is to create a Java anonymous class which 50% of the Java folks will argue against thereby we will import the OnClickListener interface to be able to listen to the click of the button. Next to start the activity in Android, we need to create an Intents which could be public (can be called from any application) or private (used within a single application). Furthermore, we need to declare our new activity inside AndroidManifest.xml file.
Other noteworthy things illustrated during the course of this chapter are:
We can debug the android application by printing the messages to the log file. Logcat is system level logging capability and can be filtered by application, pid, severity level, activity.
· Since density of screen keeps changes, pixels has no meaning. dp is better than px as it specifies density independent pixels.