Views and Layouts



One of the most important parts of any application’s design and development is the graphical user
Interface (GUI) and screen layout design. Many Android applications are popular because of their visual design, animated graphics, and easy- or fun-to-use interfaces.

In Google Android, to interface with the smartphone, tablet, or iTV screen, you use two core Java
classes. These are two of the most important and often used classes in Android development:


  • The View class (Hierarchy: java.lang.Object ➤ android.view.View)

  • The ViewGroup class (java.lang.Object ➤ android.view.View ➤ android.view.ViewGroup)


View and ViewGroup are core, high-level classes, created or subclassed from the Java Object class,
as are all Java classes. As can be seen, the View class is taken from (subclassed from) the Java
language Object class and the ViewGroup is then subclassed from the View superclass. So Views
are the highest level view objects in Android and ViewGroups are more specialized view objects
because they are farther down in the hierarchy.

View objects are created using the View class. The View class also can be used to create many
lower-level, or more customized, Java classes. Those classes that are subclassed from the View
class, such as the ViewGroup class, inherit the characteristics of their superclass.

View Class:

There may be one or more View objects that make up the entire display screen, depending on how you  use the View and ViewGroup classes to create the UI structure for your Android application’s screen.

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 that are available to Android application programmers:

  • Bounds (measurements)
  • Layout on the screen (position)
  • Order in which its layers are drawn (compositing)
  • Scrolling (directional movement)
  • Focus (which screen elements are currently active)
  • Keystroke interactions
  • Gesture interactions

ViewGroup class:

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.



Direct subclasses of the ViewGroup class include AbsoluteLayout, RelativeLayout, FrameLayout,
GridLayout, LinearLayout, ViewPager, PagerTitleStrip, AdapterView, FragmentBreadCrumbs, and
SlidingDrawer.  Thislayout class can be used to greatly expand your Android screen real estate by 100% by adding another screen that can be pulled in from offscreen.


Defining Screen Layouts: Using XML

The primary way of defining screen layouts is via XML. This screen definition XML goes inside a file you define in your Android Application Project creation process.It was called activity_hello.xml, placed inside a folder called /res/layout within your project folder. 

Layouts are important to your Android applications, which is why Layouts have their very own
layout folder in the standard project resource folder architecture for Android.
Once this activity_hello.xml file was in place, with your XML screen layout (UI) definition inside it, you then used the Java onCreate() method to push it onto your screen on the startup of your application activity.


Setting Screen Layout:

public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity hello);
}

A public method is one that is open (accessible) to any part of your Android application. A void method is one that completes a task without returning any value or data.

The words that follow the method name (always enclosed in parentheses) are the data parameters
that an application can pass to the method for its use. Parameters are chunks of data that the
method needs to complete its processing tasks.

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 to their previous
positions 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 element user settings, which UI element has the focus, and similar attributes that define the current user (usage) settings or state of use of your screen user interface elements.

The onCreate() method will always be called by the Android operating system when any activity
(remember that all activities for your app are defined in the AndroidManifest.xml file) is started. This
part of your code is where all of your initializations and UI definitions will be performed, so it must be present—at least if you need your users to interact with the Android device’s screen area.

No comments:

Post a Comment