One of the most important parts of any application's design and development is the graphical user interface (GUI) and screen layout design. Android applications are popular because of their visual design, animated graphics, and easy- or fun-to-use interfaces. We will explore the Java classes that provide the core foundation for all of these front-end capabilities in this chapter.
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Android views and layouts-chapter4
1. Chapter 4
Views and Layouts
By
Dr. Ramkumar Lakshminarayanan
Objectives
4.1. To understand the Views in Android Application
4.2. To understand the ViewGroup in Android Application
Introduction
One of the most important parts of any application's design and development is the
graphical user interface (GUI) and screen layout design. Android applications are popular
because of their visual design, animated graphics, and easy- or fun-to-use interfaces. We will
explore the Java classes that provide the core foundation for all of these front-end capabilities in
this chapter.
Android View
Two of the most important and often used classes in Android development:
The View class
The ViewGroup class
View and View Group are core, high-level classes, created or sub classed from the Java
Object class, as are all Java classes. View objects are created using the View class. The View
class can also be used to create many lower-level, or more customized, Java classes. Those
classes that are sub classed from the View class inherit the characteristics of their superclass.
So, the basic screen layout in Android is controlled by a View object, which contains a
complex data structure that represents the content and layout parameters for a given rectangular
section of the smartphone's display screen.
Using the View Class
There may be one or more View objects that make up the entire display screen.
Depending on UI structure of Android applications screen View and ViewGroup classes are
created.
Each View object controls and references its own rectangular view parameters, allowing
you to control many attributes. Here are just some examples of the many attributes controlled by
the View class parameters available to programmers:
2. Bounds (measurements)
Layout on the screen
Order in which its layers are drawn
Scrolling
Focus
Keystroke interactions
Gesture interactions
Finally, Views have the ability to receive events—interaction events between the
application's end user and the View object itself. For this reason, the View class is the logical
Java construct to subclass to build more detailed and specific UI elements, such as buttons, check
boxes, radio buttons, and text fields.
The View class serves as the foundation for UI elements that are subclasses of the View
class. Recall that in Java, a subclass is a more specific or detailed implementation of the class
from which it is subclassed. For instance, the Button class is subclassed from the TextView class,
which is subclassed from the View class, which is subclassed from the Object class. The Button
class is subclassed from the TextView class because the Button has a TextView label and is thus
a more specialized version of a TextView; that is, it is a clickable TextView with a button
background appearance.
So many UI classes have been subclassed from the View class that there is a name for
them: widgets. All of these widgets are contained in a package (a collection of classes) called
android.widget. For example, Button class can be accessed via this package using
android.widget.button.
Using the ViewGroup Class for nesting views
One of the most useful classes subclassed from the View class is the ViewGroup class.
The ViewGroup class is used to subclass layout container classes, which allow groups of View
objects to be logically grouped, arranged, and cascaded onto the screen.
ViewGroups are layout containers, usually collections of UI elements. In the diagram in
Figure 4.1, View could mean a button, a text field, a check box, and so on. This applies to any
other type of UI element.
3. Figure 4.1 ViewGroups and nested Views and ViewGroups
Direct subclasses of the ViewGroup class include AbsoluteLayout, RelativeLayout,
FrameLayout, LinearLayout, and SlidingDrawer
In the diagram in Figure 4.1, the top level ViewGroup object is the parent of the View
objects and ViewGroup objects underneath it, which are called its children. The ViewGroup
object in the second row is both a child as well as a parent, and the same goes for the ViewGroup
object in the third row.
ViewGroup objects can contain other ViewGroup objects, but View objects cannot
contain other objects. They are the end object, and are simply UI components for which you can
set via a plethora of configuration parameters.
The primary way of defining screen layouts ( ViewGroup objects) is via XML. This
XML goes inside a file called main.xml, placed inside a folder called /res/layout within
application project folder.
Once this main.xml file is in place, with the XML screen layout (UI) definition inside it,
it can be used in the Java onCreate() method to push it onto the screen startup of the application
activity.
Setting Up for Your Screen Layout
Just three lines of Java code inside an onCreate() method set your content view to the
main.xml screen layout XML definition.
4. The words before the method name determine who can access its methods and data. A
public method is one that is open to any part of your Android application.
The words that follow the method name (always enclosed in parentheses) are the
parameters that an application can pass to the method for its use. Parameters are chunks of data
the method needs to do its job.
The savedInstanceState object is a Bundle object, which is a collection of all of the states
for your activity screen UI elements. It exists so that the screen UI elements can be restored if the
screen is replaced by navigation to other screens during the use of your application. The state of
a UI screen consists of its attributes and their values, including the UI elements it uses, which
one has the focus, the colors, and similar attributes that define its appearance.
The Activity class saves your state for you, by simply extending it, and it does the work
in the application.
The super keyword calls the superclass (the class containing the onCreate() method that
was subclassed from android.app.Activity), so it is basically referencing the onCreate() method
of the android.app.Activity class from which our activity class was subclassed. It's just a shortcut
for android.app.Activity.onCreate(savedInstanceState). Since it is called from this activity class,
it affects this activity locally and applies to this activity only. This savedInstanceState object is
the one Android kindly saves for us when it deals with saving state.
The onCreate() method will always be called by the Android operating system when any
activity (defined in the AndroidManifest.xml ) file is started. This part of the code is where all of
the initializations and UI definitions will be performed, so it must be present—at least the
application need for your users to interact with the smartphone screen area.
The way that layouts contain other nested layouts in XML code (as shown in Figure 4.1)
is by nesting them inside each other. The closing tags are nested at the bottom of these structures,
and they must be nested in the correct order to show Android which layouts are inside of which
other layouts. Layouts underneath or inside of another layout conform to, and are controlled by,
their parent layout container.
The most commonly used layout container in Android: the linear layout. The
LinearLayout class, which has been subclassed from the ViewGroup class, which is subclassed
from the View class, which is subclassed from the Object class.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
5. Chapter Summary
In this chapter we discussed about the View and View Groups. Android provides a
simple way to create layout files in XML as resources provided in the res/layout project
directory. This is the most common and convenient way to build Android user interfaces and is
especially useful for defining static screen elements and control properties. In the next chapter
we discuss about creating layout for android application.