4.0 User Interface
In an Android application, the user interface is built using View and ViewGroup objects. There are many types of views and view groups, each of which is a descendant of the View class.
When we look at the documentation, the View class is declared as:View
extend Object
implements Drawable.Callback KeyEvent.Callback
AccessibilityEventSource
And the ViewGroup is declared as:ViewGroup
extends View
implements ViewManager ViewParent
View objects are the basic units of user interface expression on the Android platform. The View class serves as the base for subclasses called "widgets," which offer fully implemented UI objects, like text fields and buttons. The ViewGroup class serves as the base for subclasses called "layouts," which offer different kinds of layout architecture, like linear, tabular and relative.
A View object is a data structure whose properties store the layout parameters and content for a specific rectangular area of the screen. A View object handles its own measurement, layout, drawing, focus change, scrolling, and key/gesture interactions for the rectangular area of the screen in which it resides. As an object in the user interface, a View is also a point of interaction for the user and the receiver of the interaction events.
On the Android platform, you define an Activity's UI using a hierarchy of View and ViewGroup nodes, as shown in the diagram above. This hierarchy tree can be as simple or complex as you need it to be, and you can build it up using Android's set of predefined widgets and layouts, or with custom Views that you create yourself.
In order to attach the view hierarchy tree to the screen for rendering, your Activity must call the setContentView() method and pass a reference to the root node object: to inflate within an Activity, usually within the onCreate() method, as setContentView() method accepts a resource identifier @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
or as in the following example, to assign a user interface to an Activity, we can call setContentView() method from the onCreate() method of our Activity. In the example TextView is used as the Activity's user interface: @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
The Android system receives this reference and uses it to invalidate, measure, and draw the tree. The root node of the hierarchy requests that its child nodes draw themselves � in turn, each view group node is responsible for calling upon each of its own child views to draw themselves. The children may request a size and location within the parent, but the parent object has the final decision on where how big each child can be. Android parses the elements of your layout in-order (from the top of the hierarchy tree), instantiating the Views and adding them to their parent(s). Because these are drawn in-order, if there are elements that overlap positions, the last one to be drawn will lie on top of others previously drawn to that space.
Full List of Android Tutorials
25. Activity Life Cycle
24. Media
23. HTTP Internet Connection
22. Threads
21. Content Provider
20. Notification and Service
19. Animation - Frame By Frame, Layout, and View
18. Manifest
17. Map View
16. Preferences
15. Configuring Rotation
14. On Notepad Examples
13. Intent
12. Activity Testing
11. Android JUnit Test
10. Menus
9. TabWidget, Flipper, and SlidingDrawer
8. DatePicker, TimePicker, and Clocks
7. Advanced ListView Widget
6. ListView, Spinner
5. Layouts
4. User Interface
3. Back to Hello World Again
2. Hello World
1. Introduction
No comments:
Post a Comment