Understanding View and ViewGroup in Android
ViewGroup in Android
This is a detail tutorial that focuses on understanding View and ViewGroup in Android. The Android View category and ViewGroup category are two highly essential categories in Android apps. An Android app is made up of one or more activities. An Android activity is a display, akin and related to windows in a desktop application. In an android activity you can obtain GUI constituents. The GUI constituents are examples of View or ViewGroup subcategories. You may find it difficult to comprehend at this stage but as we go on in our explanation, I am sure the points will get clearer to you.
View Category in Android
The View category is a fantastic category for all GUI constituents in Android. For example, the TextView class which is used to show text tags in Android apps is a subcategory of View. Android is made up of the regularly utilized View subcategories:
- TextView category
- EditText category
- ImageView category
- ProgressBar category
- Button category
- ImageButton category
- CheckBox category
- DatePicker category
These are only a few of the abundant subcategories of the View category.
The ViewGroup category is a subcategory of the View class. ViewGroup examples function like containers for View examples to group View examples together. Android is made up of the following mostly utilized ViewGroup subcategories:
- LinearLayout category
- RelativeLayout category
- ListView category
- GridView category
There other subcategories of ViewGroup subcategories in Android but those listed here are the ones that are more frequently used.
The ViewGroup subcategories listed above group View examples in totality and jointly and handles their layout. For example, the LinearLayout will make the constituents after every other one either flat or perpendicularly.
A ViewGroup is a unique view that can be made up of other views (known as children.) The view group is the base category for layouts and views carriers. This category as well defines the ViewGroup.LayoutParams category which serves as the fundamental category for layouts parameters.
For extra information about producing user interface layouts, knowing more about XML Layouts developer guide.
With the use of Android’s XML vocabulary, you can swiftly design UI layouts and the screen elements they are made up of, in a like manner, you create web pages in HTML – with a progression of coated elements.
Each layout file ought to be made up of precisely one root element, which ought to be a View or ViewGroup object. Immediately, you have defined the root element, you can add extra layout objects or widgets as child elements to steadily create a View hierarchy that defines your layout.
As a general rule, the XML vocabulary for making declaration of UI elements very much follows the arrangement and naming of the categories and procedures, where element names match category names and attribute names match procedures.
Below is an implementation of a customized ViewGroup that carries out a simple FrameLayout together with the capacity to heap children in left and right gutters.
FrameLayout is created to block out an area on the screen to display one single item. As a general rule, FrameLayout ought to be used to hold one unique child view, due to the fact that it can be difficult to plan child views in a manner that’s scalable to diverse screen sizes without the children partly covering each other.
You can, nevertheless, add numerous children to a FrameLayout and have power over their position within the FrameLayout by passing on gravity to every child, with the use of the android:layout_gravity attribute.
Child views are drawn in a stack, with the most newly added child on the apex. The size of the FrameLayout is the size of its biggest child (in addition to padding), visible or invisible (if the FrameLayout’s parent allows). Views that are GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.
A rundown of the nested categories
[table id=1 /]
[table id=2 /]
Designing for Different Android Devices
The term “Android device” covers an enormous array of tablet and smartphone products with various screen sizes and resolutions.
Owing to this, application user interfaces must currently be cautiously designed to make sure that there is accurate presentation on as broad range of display sizes as possible.
A major section of this is ensuring that the user interface layouts resize accurately when run on various devices. This can to a great to be attained through careful organisation and the utilization of the layout managers.
It is as well crucial to keep in mind that the greater part of Android based smartphone and tablets can be held by the user in both portrait and landscape directions.
A properly-designed user interface ought to be able to adapt to changes like that and make sensible layout alterations to make use of the obtaining screen space in every direction held or orientation.
Views and View Groups
Each piece in a user interface is a subcategory of the Android View category (to be exact android.view.View). The Android SDK makes available in a set of pre-created views that can be utilized to create a user interface.
Typical examples are regular items like the Button, CheckBox, ProgressBar and TextView classes. Views like that are as well known as widgets.
For conditions that are not met by the widgets provided with the SDK, fresh views may be produced either by sub categorizing and extending an existing class, or producing a completely fresh constituent by creating directly on top of the View category.
A view can as well be made up of various other views (as well referred to as composite view). Vies like that are sub categorized from the Android ViewGroup class (android.view.ViewGroup) which is in itself a subcategory of View.
An instance of a view like that is the RadioGroup, which is aimed to be composed of variable RadioButton objects in a manner that only one can be in the “on” location at any one time.
With structure, amalgamated views made up of one parent view (obtained from the ViewGroup class and also referred to as a container view or root element) that is capable of containing other views (known as child views). Another category of ViewGroup based container view is that of the layout manager.
Android Layout Managers
In addition to the widget style views, the SDK as well includes a set of views known as layouts. Layouts are container views (and, thus, subcategorized from ViewGroup) structured for the main intention of controlling the way a child views are placed on the screen.
The Android SDK is made up of the following layout views that may be utilized in an Android user interface design:
This places child views in one row or column depending on the orientation chosen. A weight value can be set on every child to show how much of the layout space that child ought to inhabit in relation to other children.
This organizes child views into a grid format of rows and columns. Every row within a table is shown by a TableRow object child, which, in turn, is made up of a view object for every cell.
The reason or aim of the FrameLayout is to assign a section of display, basically for the intentions of showing one single view.
If variable child views are affixed they will, by default, become visible on top of each other positioned in the top left hand corner of the layout area.
Every other positioning of individual child views can be attained by setting gravity values on each child. For instance, setting a center_vertical gravity on a child will cause it to be placed in the vertical center of the containing FrameLayout view.
In all probability as the most influential and flexible of the layout managers, this permits child views to be placed in relation to both each other and the bearing layout view via the measurement of alignments and margins on child views.
For instance, child View A may be configured to be placed in the vertical and horizontal core of the bearing RelativeLayout view. View B, on the contrary may as well be configured to be positioned horizontally inside the layout view, but placed 30 pixels above the top edge of View A, thus making the vertical position in relation to that of View A.
The RelativeLayout manager can be composed of specific utilization when planning a user interface that ought to function on a lot of screen sizes and orientations.
This permits child views to be placed at particular X and Y coordinates inside the bearing layout view. This layout is used to discourage for the fact that it is devoid of the flexibility to react to alterations in screen size and orientation.
The GridLayout is a comparatively new layout manager that was initiated as a section of Android 4.0. A GridLayout example is divided by invisible lines that outline a grid being rows and columns of cells.
Child views are at this point situated in cells and may be configured to envelop a lot of cells both horizontally and vertically permitting a broad range of layout options to be swiftly and simply carried out.
Gaps between constituents in a GridLayout may be carried out by placing a unique type of view known as Space view into adjacent cells, or by setting margin parameters.
When taking into consideration the use of layouts in the user interface for an Android application, it is worth bearing in mind that, these can be nested inside each other to produce a user interface design of roughly about any essential level of complexity.