public abstract class

View

extends Object
implements Alignment StretchMode
java.lang.Object
   ↳ com.neomades.ui.View
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

Abstract UI element (View).

Important : You must not override a View in order to customize an UI element.

Stretch mode

Each view could manage its size belong the content or the parent.

The setStretchMode() method could use :

  • MATCH_CONTENT : the size will cover the content of the view
  • MATCH_PARENT : the size will cover more than the content of the view
    • The content size + empty size from the layout available for this view (computed with coefficients of Layout and its children)
Special Combinations

A parent with MATCH_CONTENT which contains a child with MATCH_PARENT will have the same dimension as the child (like the child in MATCH_CONTENT mode).

Dimensioning

The Generic API enables to set the dimensions of each view (width, height, margin, padding). The dimensions must be set in DP (density independent pixel). It represents a virtual pixel unit that you must use to express dimensions or positions in a density-independent way.

Layout Alignment

A Layout could contains many views, each of these views could manage its alignment from the layout.

Content Alignment

Some of view (eg. Button) have content that can be aligned

Summary

XML Attributes
Attribute Name Related Method Description
background setBackground(int) Res.background.XXXX  
backgroundColor setBackgroundColor(Color) #AARRGGBB or a color  
backgroundImage setBackgroundImage(Image) Res.image.XXXX or a color  
borderColor setBorderColor(Color) #AARRGGBB or a color  
borderWidth setBorderWidth(int) eg: 10dp or 10 (dp is the default unit)  
clickable setClickable(boolean) If the view could be clicked  
cornerRadius setCornerRadius(int) eg: 10dp or 10 (dp is the default unit)  
enabled setEnabled(boolean) If the view could be enabled  
focusable setFocusable(boolean) If the view could be focused  
height setHeight(int) eg: 10dp or 10 (dp is the default unit)  
id setId(int) Res.id.XXXX  
layoutAlignment setLayoutAlignment(int) A combination of alignment values (eg: TOP|LEFT)  
margin setMargin(int) eg: 10dp or 10 (dp is the default unit)  
marginBottom setMarginBottom(int) eg: 10dp or 10 (dp is the default unit)  
marginLeft setMarginLeft(int) eg: 10dp or 10 (dp is the default unit)  
marginRight setMarginRight(int) eg: 10dp or 10 (dp is the default unit)  
marginTop setMarginTop(int) eg: 10dp or 10 (dp is the default unit)  
padding setPadding(int) eg: 10dp or 10 (dp is the default unit)  
paddingBottom setPaddingBottom(int) eg: 10dp or 10 (dp is the default unit)  
paddingLeft setPaddingLeft(int) eg: 10dp or 10 (dp is the default unit)  
paddingRight setPaddingRight(int) eg: 10dp or 10 (dp is the default unit)  
paddingTop setPaddingTop(int) eg: 10dp or 10 (dp is the default unit)  
stretchHorizontalMode setStretchHorizontalMode(int) The dimension could be adapted to the parent dimension or to the content size. 
stretchVerticalMode setStretchVerticalMode(int) The dimension could be adapted to the parent dimension or to the content size. 
visible setVisible(boolean) If the view is visible or hidden and collapsed  
width setWidth(int) eg: 10dp or 10 (dp is the default unit)  
[Expand]
Inherited Constants
From interface com.neomades.ui.Alignment
From interface com.neomades.ui.StretchMode
Protected Constructors
View()
Internal
Public Methods
View findView(int viewId)
Find view by Id.
int getId()
Returns the identifier of this element, by default all elements return "0".
Object getTag()
Returns this view's tag.
static View inflateXML(int layoutResId)
Inflate view from XMLLayout declared in the URS File.
static View inflateXML(InputStream xmlStream)
@return
boolean isClickable()
Returns if the view is clickable.
boolean isEnabled()
Return a boolean indicating if the view is enabled or not
boolean isFocusable()
Returns if the view is focusable (for the devices that have a keyboard Ui navigation).
boolean isVisible()
Returns if the view is visible
void runOnUiThread(Runnable action)
Calls action.run() asynchronously on the UI-Thread.
void setBackground(Background background)
Sets the background on this view.
void setBackground(int backgroundResId)
Sets the background on this view with a XML background definition.
void setBackgroundColor(Color color)
Sets the background color on this view.
void setBackgroundImage(int imageID)
Set the background image for this view.
void setBackgroundImage(Image image)
Set the background image for this view.
void setBorderColor(Color borderColor)
Sets the border color on this view.
void setBorderWidth(int widthInDp)
Set the border width (size) in DP units.
void setClickListener(ClickListener clickListener)
Sets a ClickListener to this view.
void setClickable(boolean clickable)
Sets this view clickable.
void setCornerRadius(int radiusSize)
Set the four corners radius, if the view has corners.
void setDrawingCacheEnabled(boolean enabled)
Enables or disables the drawing cache.
void setEnabled(boolean enabled)
Sets the state of this view to enabled or disabled.
void setFocusable(boolean focusable)
Sets the view focusable.
void setGestureDetector(GestureDetector detector)
Sets a GestureDetector on this view.
void setHeight(int heightDp)
Sets the height of the view, in dp units (density-independent pixels).
void setId(int id)
Sets a identifier/tag to this element
void setInvalidateDisabled(boolean disabled)
If set to true, force the layout that contains the view to not be draw again when its content changes.
void setLayoutAlignment(int alignment)
Sets the view alignment into its parent.
void setMargin(int uniformMarginDp)
Set the margin outside this view (used by Layout), by setting on each side the given value.
void setMargin(int leftDp, int topDp, int rightDp, int bottomDp)
Set the margin outside this view (used by Layout).
void setMarginBottom(int marginBottomDp)
Sets the bottom margin with the given value.
void setMarginLeft(int marginLeftDp)
Sets the left margin with the given value.
void setMarginRight(int marginRightDp)
Sets the right margin with the given value.
void setMarginTop(int marginTopDp)
Sets the top margin with the given value.
void setPadding(int leftDp, int topDp, int rightDp, int bottomDp)
Sets the view padding.
void setPadding(int uniformPaddingDp)
Sets the view padding, with for each side the value given in parameter.
void setPaddingBottom(int paddingBottomDp)
Sets the bottom padding with the given value.
void setPaddingLeft(int paddingLeftDp)
Sets the left padding with the given value.
void setPaddingRight(int paddingRightDp)
Sets the right padding with the given value.
void setPaddingTop(int paddingTopDp)
Sets the top padding with the given value.
void setSize(int widthDp, int heightDp)
Sets the size of the view, in dp units (density-independent pixels).
void setStretchHorizontalMode(int widthMode)
Sets the stretch horizontal mode.
void setStretchMode(int horizontalMode, int verticalMode)
Sets the view content stretch mode.
void setStretchVerticalMode(int heightMode)
Sets the stretch vertical mode.
void setStyle(int styleResId)
void setStyle(Style style)
void setTag(Object tag)
Sets the tag associated with this view.
void setTouchListener(TouchListener touchListener)
Register a listener to be invoked when a touch event is dispatched to this view.
void setTransformOrigin(float xOrigin, float yOrigin)
Sets the x and y location of the point around which the view will be scaled or rotated when an animation occurs.
void setVisible(boolean visible)
Sets the view's visibility : visible or invisible/hidden
void setWidth(int widthDp)
Sets the width of the view, in dp units (density-independent pixels).
[Expand]
Inherited Methods
From class java.lang.Object

XML Attributes

background

Res.background.XXXX

Related Methods

backgroundColor

#AARRGGBB or a color

Related Methods

backgroundImage

Res.image.XXXX or a color

Related Methods

borderColor

#AARRGGBB or a color

Related Methods

borderWidth

eg: 10dp or 10 (dp is the default unit)

Related Methods

clickable

If the view could be clicked

Related Methods

cornerRadius

eg: 10dp or 10 (dp is the default unit)

Related Methods

enabled

If the view could be enabled

Related Methods

focusable

If the view could be focused

Related Methods

height

eg: 10dp or 10 (dp is the default unit)

Related Methods

id

Res.id.XXXX

Related Methods

layoutAlignment

A combination of alignment values (eg: TOP|LEFT)

Related Methods

margin

eg: 10dp or 10 (dp is the default unit)

Related Methods

marginBottom

eg: 10dp or 10 (dp is the default unit)

Related Methods

marginLeft

eg: 10dp or 10 (dp is the default unit)

Related Methods

marginRight

eg: 10dp or 10 (dp is the default unit)

Related Methods

marginTop

eg: 10dp or 10 (dp is the default unit)

Related Methods

padding

eg: 10dp or 10 (dp is the default unit)

Related Methods

paddingBottom

eg: 10dp or 10 (dp is the default unit)

Related Methods

paddingLeft

eg: 10dp or 10 (dp is the default unit)

Related Methods

paddingRight

eg: 10dp or 10 (dp is the default unit)

Related Methods

paddingTop

eg: 10dp or 10 (dp is the default unit)

Related Methods

stretchHorizontalMode

The dimension could be adapted to the parent dimension or to the content size. View's size is prior than stretch mode.

stretchVerticalMode

The dimension could be adapted to the parent dimension or to the content size. View's size is prior than stretch mode.

Related Methods

visible

If the view is visible or hidden and collapsed

Related Methods

width

eg: 10dp or 10 (dp is the default unit)

Related Methods

Protected Constructors

protected View ()

Internal

Public Methods

public View findView (int viewId)

Find view by Id.

Parameters
viewId the view with the Id
Returns
  • the first view with the corresponding id or null if not found
See Also

public int getId ()

Returns the identifier of this element, by default all elements return "0".

Related XML Attributes
Returns
  • the identifier of this view or "0" if none
See Also

public Object getTag ()

Returns this view's tag.

Returns
  • tag the Object stored in this view as a tag

public static View inflateXML (int layoutResId)

Inflate view from XMLLayout declared in the URS File.

Parameters
layoutResId layout resId from the Resources class
Returns
  • the inflated view or null
Throws
ResourceNotFoundException if the layout has not been found
CrossThreadException if the method is called from outside the UI-Thread

public static View inflateXML (InputStream xmlStream)

@return

Throws
ResourceNotFoundException
CrossThreadException
ResourceNotFoundException

public boolean isClickable ()

Returns if the view is clickable.

Some views are not clickable (see the specific view documentation in order to know if the view clickable property could be changed)

Related XML Attributes
Returns
  • true if the view is clickable

public boolean isEnabled ()

Return a boolean indicating if the view is enabled or not

Related XML Attributes
Returns
  • true if the view is enabled, false if it is disabled

public boolean isFocusable ()

Returns if the view is focusable (for the devices that have a keyboard Ui navigation).

For other platforms or devices, this method will always return false.

Related XML Attributes
Returns
  • true if the view is focusable

public boolean isVisible ()

Returns if the view is visible

Related XML Attributes
Returns
  • true if the view is visible

public void runOnUiThread (Runnable action)

Calls action.run() asynchronously on the UI-Thread. This method should be used when an application thread needs to update the GUI.

If already on the UI-Thread, the Runnable is run immediately.

Parameters
action the action to execute in the UI-Thread

public void setBackground (Background background)

Sets the background on this view.

Parameters
background the new background or null to set the default platform background
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setBackground (int backgroundResId)

Sets the background on this view with a XML background definition.

Related XML Attributes
Parameters
backgroundResId resource identifier from Res
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setBackgroundColor (Color color)

Sets the background color on this view. To reset the native default background, use setBackground(Background) with null .

Related XML Attributes
Parameters
color the color to set
Throws
CrossThreadException if the method is called from outside the UI-Thread
NullPointerException if the color parameter is null

public void setBackgroundImage (int imageID)

Set the background image for this view.

If the size of the image is different than the view size, then the image will be stretched in order to fill the view's background.

Parameters
imageID the id of the image to use for the background
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setBackgroundImage (Image image)

Set the background image for this view.

If the size of the image is different than the view size, then the image will be stretched in order to fill the view's background.

To reset the native default background, use setBackground(Background) with null.

Related XML Attributes
Parameters
image the image to use for the background (null to remove the current image)
Throws
CrossThreadException if the method is called from outside the UI-Thread
NullPointerException if the image parameter is null

public void setBorderColor (Color borderColor)

Sets the border color on this view.

Related XML Attributes
Parameters
borderColor the new color or null to set the default platform color
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setBorderWidth (int widthInDp)

Set the border width (size) in DP units.

To disable border, the border width should be set to zero.

Related XML Attributes
Parameters
widthInDp width of the border in DP units
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setClickListener (ClickListener clickListener)

Sets a ClickListener to this view.

When the view is clicked, the onClick() method from ClickListener will be called, with the clicked view as parameter.

If the view is not clickable, it becomes clickable (even if the listener is null).

Parameters
clickListener The listener to set on the view (or null to remove the current listener)

public void setClickable (boolean clickable)

Sets this view clickable.

Related XML Attributes
Parameters
clickable true if the view should be clickable
Throws
CrossThreadException if the method is called from outside the UI-Thread
See Also

public void setCornerRadius (int radiusSize)

Set the four corners radius, if the view has corners. The value of the radius must be set in dp.

To disable corners radius, the radius size should be set to zero.

Related XML Attributes
Parameters
radiusSize radius of the corner in dp
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setDrawingCacheEnabled (boolean enabled)

Enables or disables the drawing cache.

Parameters
enabled true to enable the drawing cache, false otherwise

public void setEnabled (boolean enabled)

Sets the state of this view to enabled or disabled.

Related XML Attributes
Parameters
enabled true if the View is enabled
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setFocusable (boolean focusable)

Sets the view focusable.

(Only available for the devices that have a keyboard UI navigation)

Related XML Attributes
Parameters
focusable true if the view is focusable
Throws
CrossThreadException if the method is called from outside the UI-Thread
See Also

public void setGestureDetector (GestureDetector detector)

Sets a GestureDetector on this view. A same GestureDetector can be reused on multiple views.

Parameters
detector the detector, or null to remove the previously set detector

public void setHeight (int heightDp)

Sets the height of the view, in dp units (density-independent pixels). Note that if a height is set, vertical content stretch mode is ignored.

The height is prior than the vertical stretch mode : if the view is in MATCH_PARENT vertically but with a fixed height, then the height will be used.

Related XML Attributes
Parameters
heightDp the height to set in dp
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setId (int id)

Sets a identifier/tag to this element

Related XML Attributes
Parameters
id the identifier
Throws
CrossThreadException if the method is called from outside the UI-Thread
See Also

public void setInvalidateDisabled (boolean disabled)

If set to true, force the layout that contains the view to not be draw again when its content changes. The view will be redraw with its new content but its layout will not be recalculated. Consequently if the content is too large or too small for the view, it may result in a undesirable display. This behavior allows you to ignore layout updates, which is usually better for performance.

Be sure to reset it to false to activate again the default behavior.

Parameters
disabled activate / deactivate the automatic calculation and drawing of the layout's view

public void setLayoutAlignment (int alignment)

Sets the view alignment into its parent.

If the stretch mode is not MATCH_PARENT, then the layout alignment could set the alignment of this view into its parent. If the stretch mode is MATCH_PARENT , the layout alignment is not taken into account until the stretch mode is set to MATCH_CONTENT

e.g. A button with MATCH_CONTENT in widthMode and inserted into a VerticalLayout could alignement into the layout on the LEFT, HCENTER or RIGHT

If this view is into VerticalLayout, then the layout alignment will have effect only for horizontal alignment. To align this view vertically, see the setContentAlignment(int) method and the conditions to align vertically a child in a VerticalLayout.

If this view is into HorizontalLayout, then the layout alignment will have effect only for vertical alignment. To align this view horizontally, see the setContentAlignment(int) method and the conditions to align horizontally a child in a HorizontalLayout.

Note : this property is used if the view is a child of a Layout

Related XML Attributes
Parameters
alignment alignment of this in relation with its parent (Layout)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setMargin (int uniformMarginDp)

Set the margin outside this view (used by Layout), by setting on each side the given value.

Cross-Platform consideration

In Android, be sure to apply a layout alignment to the view if it is a LinearLayout. If not, the margin may not apply.

Related XML Attributes
Parameters
uniformMarginDp the margin to set on each side (in dp).
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setMargin (int leftDp, int topDp, int rightDp, int bottomDp)

Set the margin outside this view (used by Layout).

Cross-Platform consideration

In Android, be sure to apply a layout alignment to the view if it is a LinearLayout. If not, the margin may not apply.

Parameters
leftDp value of the left margin (in dp)
topDp value of the top margin (in dp)
rightDp value of the right margin (in dp)
bottomDp value of the bottom margin (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setMarginBottom (int marginBottomDp)

Sets the bottom margin with the given value.

Cross-Platform consideration

In Android, be sure to apply a layout alignment to the view if it is a LinearLayout. If not, the margin may not apply.

Related XML Attributes
Parameters
marginBottomDp value of the bottom margin (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setMarginLeft (int marginLeftDp)

Sets the left margin with the given value.

Cross-Platform consideration

In Android, be sure to apply a layout alignment to the view if it is a LinearLayout. If not, the margin may not apply.

Related XML Attributes
Parameters
marginLeftDp value of the left margin (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setMarginRight (int marginRightDp)

Sets the right margin with the given value.

Cross-Platform consideration

In Android, be sure to apply a layout alignment to the view if it is a LinearLayout. If not, the margin may not apply.

Related XML Attributes
Parameters
marginRightDp value of the right margin (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setMarginTop (int marginTopDp)

Sets the top margin with the given value.

Cross-Platform consideration

In Android, be sure to apply a layout alignment to the view if it is a LinearLayout. If not, the margin may not apply.

Related XML Attributes
Parameters
marginTopDp value of the top margin (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPadding (int leftDp, int topDp, int rightDp, int bottomDp)

Sets the view padding.

Parameters
leftDp the left padding (in dp)
topDp the top padding (in dp)
rightDp the right padding (in dp)
bottomDp the bottom padding (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPadding (int uniformPaddingDp)

Sets the view padding, with for each side the value given in parameter.

Related XML Attributes
Parameters
uniformPaddingDp the padding to set on each side (in dp).
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPaddingBottom (int paddingBottomDp)

Sets the bottom padding with the given value.

Related XML Attributes
Parameters
paddingBottomDp value of the bottom padding (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPaddingLeft (int paddingLeftDp)

Sets the left padding with the given value.

Related XML Attributes
Parameters
paddingLeftDp value of the left padding (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPaddingRight (int paddingRightDp)

Sets the right padding with the given value.

Related XML Attributes
Parameters
paddingRightDp value of the right padding (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPaddingTop (int paddingTopDp)

Sets the top padding with the given value.

Related XML Attributes
Parameters
paddingTopDp value of the top padding (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setSize (int widthDp, int heightDp)

Sets the size of the view, in dp units (density-independent pixels). Note that if a size is set, vertical and horizontal content stretch mode are ignored.

The size is prior than the stretch mode : if the view is in MATCH_PARENT but with a fixed size, then the size will be used.

Parameters
widthDp the width to set in dp
heightDp the height to set in dp
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setStretchHorizontalMode (int widthMode)

Sets the stretch horizontal mode.

Related XML Attributes
Parameters
widthMode MATCH_PARENT or MATCH_CONTENT
Throws
CrossThreadException if the method is called from outside the UI-Thread
IllegalArgumentException If the widthMode parameter has wrong value

public void setStretchMode (int horizontalMode, int verticalMode)

Sets the view content stretch mode.

A view could arrange its size :

  • the parent bounds
  • the content bounds.

See Content Stretch

See Layout disposition

Parameters
horizontalMode MATCH_PARENT or MATCH_CONTENT
verticalMode MATCH_PARENT or MATCH_CONTENT
Throws
CrossThreadException if the method is called from outside the UI-Thread
IllegalArgumentException if any of mode parameters has wrong value

public void setStretchVerticalMode (int heightMode)

Sets the stretch vertical mode.

Related XML Attributes
Parameters
heightMode MATCH_PARENT or MATCH_CONTENT
Throws
IllegalArgumentException If the heightMode parameter has wrong value

public void setStyle (int styleResId)

public void setStyle (Style style)

public void setTag (Object tag)

Sets the tag associated with this view.

A tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy. Tags can also be used to store data within a view without resorting to another data structure.

Parameters
tag an Object to tag the view with

public void setTouchListener (TouchListener touchListener)

Register a listener to be invoked when a touch event is dispatched to this view.

If the parameter is null, the previous listener, if exist, will be removed.

Parameters
touchListener the touch listener to attach to this view

public void setTransformOrigin (float xOrigin, float yOrigin)

Sets the x and y location of the point around which the view will be scaled or rotated when an animation occurs.

By default, the point is centered on the object. X and Y must be values between 0 and 1 where (0, 0) represents the top left corner of the view and (1, 1) the bottom right corner. The center of the view is (0.5, 0.5).

Parameters
xOrigin x location of the scale and rotation center
yOrigin y location of the scale and rotation center
Throws
IllegalArgumentException if x or y are not between 0 and 1.

public void setVisible (boolean visible)

Sets the view's visibility : visible or invisible/hidden

Related XML Attributes
Parameters
visible true if the view should be visible
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setWidth (int widthDp)

Sets the width of the view, in dp units (density-independent pixels). Note that if a width is set, horizontal content stretch mode is ignored.

The width is prioritary over the stretch mode : if the view is in MATCH_PARENT horizontally but with a fixed width, then the width will be used.

Related XML Attributes
Parameters
widthDp the width to set in dp
Throws
CrossThreadException if the method is called from outside the UI-Thread