Types of Views and Property in Android
This entry was posted on Sunday January 2, 2022This class addresses the fundamental structure block for UI parts. A View involves a rectangular region on the screen and is liable for drawing and occasion taking care of. View is the base class for gadgets, which are utilized to make intelligent UI parts (buttons, text fields, and so forth) The ViewGroup subclass is the base class for formats, which are undetectable compartments that have different Perspectives (or other ViewGroups) and characterize their design properties.
Utilizing Views
Every one of the perspectives in a window are organized in a solitary tree. You can add sees either from code or by indicating a tree of perspectives in at least one XML format records. There are many particular subclasses of perspectives that go about as controls or are fit for showing text, pictures, or other substance.
Whenever you have made a tree of perspectives, there are normally a couple of kinds of normal activities you might wish to perform:
Set properties: for instance setting the text of a TextView. The accessible properties and the techniques that set them will shift among the various subclasses of perspectives. Note that properties that are known at fabricate time can be set in the XML format records.
Set concentration: The structure will deal with moving concentration in light of client input. To drive concentration to a particular view, call requestFocus().
Set up audience members: Views permit customers to set audience members that will be informed when something fascinating happens to the view. For instance, all perspectives will allow you to set an audience to be told when the view gains or loses center. You can enroll such an audience utilizing setOnFocusChangeListener(android.view.View.OnFocusChangeListener). Other view subclasses offer more specific audience members. For instance, a Button opens an audience to tell customers when the button is clicked.
Set perceivability: You can stow away or show sees utilizing setVisibility(int).
Note: The Android structure is answerable for estimating, spreading out and drawing sees. You ought not call strategies that play out these activities on sees yourself except if you are really carrying out a ViewGroup.
Carrying out a Custom View
To carry out a custom view, you will normally start by giving abrogates to a portion of the standard techniques that the system approaches all perspectives. You don’t have to supersede these techniques. Indeed, you can begin simply by abrogating onDraw(android.graphics.Canvas).
Position
he calculation of a view is that of a square shape. A view has an area, communicated as a couple of left and top directions, and two aspects, communicated as a width and a stature. The unit for area and aspects is the pixel.
It is feasible to recover the area of a view by conjuring the strategies getLeft() and getTop(). The previous returns the left, or X, direction of the square shape addressing the view. The last option returns the top, or Y, direction of the square shape addressing the view. These strategies both return the area of the view comparative with its parent. For example, when getLeft() returns 20, that implies the view is found 20 pixels to the right of the left edge of its immediate parent.
Furthermore, a few accommodation techniques are proposed to stay away from superfluous calculations, specifically getRight() and getBottom(). These strategies return the directions of the right and base edges of the square shape addressing the view. For example, calling getRight() is like the accompanying calculation: getLeft() + getWidth() (see Size for more data about the width.)
Size, cushioning and edges
The size of a view is communicated with a width and a stature. A view really have two sets of width and stature esteems.
The primary pair is known as estimated width and estimated tallness. These aspects characterize how enormous a view needs to be inside its parent (see Layout for additional subtleties.) The deliberate aspects can be gotten by calling getMeasuredWidth() and getMeasuredHeight().
The subsequent pair is basically known as width and tallness, or now and then drawing width and drawing stature. These aspects characterize the real size of the view on screen, at drawing time and later design. These qualities may, yet don’t need to, be not quite the same as the deliberate width and stature. The width and tallness can be acquired by calling getWidth() and getHeight().
To quantify its aspects, a view considers its cushioning. The cushioning is communicated in pixels for the left, top, right and base pieces of the view. Cushioning can be utilized to counterbalance the substance of the view by a particular measure of pixels. For example, a left cushioning of 2 will move the view’s substance by 2 pixels to the right of the left edge. Cushioning can be set utilizing the setPadding(int, int, int, int) or setPaddingRelative(int, int, int, int) strategy and questioned by calling getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom(), getPaddingStart(), getPaddingEnd().
Despite the fact that a view can characterize a cushioning, it doesn’t offer any help for edges. Notwithstanding, view bunches offer such a help. Allude to ViewGroup and ViewGroup.MarginLayoutParams for additional data.
Format
Design is a two pass process: an action pass and a format pass. The estimating pass is executed in measure(int, int) and is a hierarchical crossing of the view tree. Each view pushes aspect details down the tree during the recursion. Toward the finish of the action pass, each view has put away its estimations. The subsequent pass occurs in layout(int, int, int, int) and is additionally hierarchical. During this pass each parent is liable for situating every one of its youngsters utilizing the sizes processed in the action pass.
At the point when a view’s action() technique returns, its getMeasuredWidth() and getMeasuredHeight() values should be set, alongside those for the entirety of that view’s relatives. A view’s deliberate width and estimated tallness esteems should regard the requirements forced by the view’s folks. This ensures that toward the finish of the action pass, all guardians acknowledge their youngsters’ estimations in general. A parent view might call measure() at least a time or two on its youngsters. For instance, the parent might quantify every kid once with vague aspects to discover how huge they need to be, then, at that point, call measure() on them again with genuine numbers assuming the amount of the relative multitude of youngsters’ unconstrained sizes is too large or excessively little.
The action pass utilizes two classes to impart aspects. The MeasureSpec class is utilized by perspectives to let their folks know how they need to be estimated and situated. The base LayoutParams class simply portrays how large the view needs to be for both width and tallness. For each aspect, it can determine one of: a definite number
MATCH_PARENT, which implies the view needs to be pretty much as large as its parent (short cushioning)
WRAP_CONTENT, which implies that the view needs to be sufficiently enormous to encase its substance (in addition to cushioning).
There are subclasses of LayoutParams for various subclasses of ViewGroup. For instance, AbsoluteLayout has its own subclass of LayoutParams which adds a X and Y esteem.
MeasureSpecs are utilized to push necessities down the tree from parent to youngster. A MeasureSpec can be in one of three modes:
Undefined: This is utilized by a parent to decide the ideal component of a kid view. For instance, a LinearLayout might call measure() on its youngster with the tallness set to UNSPECIFIED and a width of EXACTLY 240 to discover how tall the kid view needs to be given a width of 240 pixels.
Precisely: This is utilized by the parent to force an accurate size on the kid. The youngster should utilize this size, and assurance that each of its relatives will fit inside this size.
AT_MOST: This is utilized by the parent to force a greatest size on the kid. The youngster should ensure that it and every one of its relatives will fit inside this size.
To start a format, call requestLayout(). This strategy is normally called by a view on itself when it accepts that it can as of now not fit inside its present limits.
Drawing
Drawing is dealt with by strolling the tree and recording the drawing orders of any View that necessities to refresh. Later this, the drawing orders of the whole tree are given to screen, cut to the recently harmed region.
The tree is to a great extent recorded and attracted request, with guardians drawn previously (i.e., behind) their kids, with kin attracted the request they show up in the tree. Assuming you set a foundation drawable for a View, then, at that point, the View will move it prior to getting back to back to its onDraw() strategy. The youngster drawing request can be superseded with custom kid attracting request a ViewGroup, and with setZ(float) custom Z values} set on Views.
To constrain a view to draw, call negate().
Occasion Handling and Threading
The fundamental pattern of a view is as per the following:
An occasion comes in and is dispatched to the proper view. The view handles the occasion and advises any audience members.
On the off chance that over the span of handling the occasion, the view’s limits might should be changed, the view will call requestLayout().
Also, if throughout handling the occasion the view’s appearance might should be changed, the view will call nullify().
In the event that either requestLayout() or discredit() were called, the system will deal with estimating, spreading out, and drawing the tree as suitable.
Note: The whole view tree is single strung. You should forever be on the UI string when calling any technique on any view. Assuming you are taking care of business on different strings and need to refresh the condition of a view from that string, you should utilize a Handler.
Center Handling
The structure will deal with routine center development in light of client input. This incorporates changing the concentration as perspectives are eliminated or stowed away, or as new perspectives become accessible. Sees demonstrate their eagerness to take center through the isFocusable() technique. To change whether a view can take center, call setFocusable(boolean). When in touch mode (see notes beneath) sees demonstrate whether they actually would like center by means of isFocusableInTouchMode() and can change this through setFocusableInTouchMode(boolean).
Center development is based around a calculation which finds the closest neighbor in a provided guidance. In uncommon cases, the default calculation may not coordinate