android.support.v4.view
Class ViewCompat

java.lang.Object
  extended by android.support.v4.view.ViewCompat

public class ViewCompat
extends Object

Helper for accessing features in View introduced after API level 4 in a backwards compatible fashion.


Field Summary
static int IMPORTANT_FOR_ACCESSIBILITY_AUTO
          Automatically determine whether a view is important for accessibility.
static int IMPORTANT_FOR_ACCESSIBILITY_NO
          The view is not important for accessibility.
static int IMPORTANT_FOR_ACCESSIBILITY_YES
          The view is important for accessibility.
static int LAYER_TYPE_HARDWARE
          Indicates that the view has a hardware layer.
static int LAYER_TYPE_NONE
          Indicates that the view does not have a layer.
static int LAYER_TYPE_SOFTWARE
          Indicates that the view has a software layer.
static int LAYOUT_DIRECTION_INHERIT
          Horizontal layout direction of this view is inherited from its parent.
static int LAYOUT_DIRECTION_LOCALE
          Horizontal layout direction of this view is from deduced from the default language script for the locale.
static int LAYOUT_DIRECTION_LTR
          Horizontal layout direction of this view is from Left to Right.
static int LAYOUT_DIRECTION_RTL
          Horizontal layout direction of this view is from Right to Left.
static int OVER_SCROLL_ALWAYS
          Always allow a user to over-scroll this view, provided it is a view that can scroll.
static int OVER_SCROLL_IF_CONTENT_SCROLLS
          Allow a user to over-scroll this view only if the content is large enough to meaningfully scroll, provided it is a view that can scroll.
static int OVER_SCROLL_NEVER
          Never allow a user to over-scroll this view.
 
Constructor Summary
ViewCompat()
           
 
Method Summary
static boolean canScrollHorizontally(android.view.View v, int direction)
          Check if this view can be scrolled horizontally in a certain direction.
static boolean canScrollVertically(android.view.View v, int direction)
          Check if this view can be scrolled vertically in a certain direction.
static AccessibilityNodeProviderCompat getAccessibilityNodeProvider(android.view.View view)
          Gets the provider for managing a virtual view hierarchy rooted at this View and reported to AccessibilityServices that explore the window content.
static int getImportantForAccessibility(android.view.View view)
          Gets the mode for determining whether this View is important for accessibility which is if it fires accessibility events and if it is reported to accessibility services that query the screen.
static int getLabelFor(android.view.View view)
          Gets the id of a view for which a given view serves as a label for accessibility purposes.
static int getLayerType(android.view.View view)
          Indicates what type of layer is currently associated with this view.
static int getLayoutDirection(android.view.View view)
          Returns the resolved layout direction for this view.
static int getOverScrollMode(android.view.View v)
          Returns the over-scroll mode for this view.
static android.view.ViewParent getParentForAccessibility(android.view.View view)
          Gets the parent for accessibility purposes.
static boolean hasTransientState(android.view.View view)
          Indicates whether the view is currently tracking transient state that the app should not need to concern itself with saving and restoring, but that the framework should take special note to preserve when possible.
static void onInitializeAccessibilityEvent(android.view.View v, android.view.accessibility.AccessibilityEvent event)
          Initializes an AccessibilityEvent with information about this View which is the event source.
static void onInitializeAccessibilityNodeInfo(android.view.View v, AccessibilityNodeInfoCompat info)
          Initializes an AccessibilityNodeInfo with information about this view.
static void onPopulateAccessibilityEvent(android.view.View v, android.view.accessibility.AccessibilityEvent event)
          Called from View.dispatchPopulateAccessibilityEvent(AccessibilityEvent) giving a chance to this View to populate the accessibility event with its text content.
static boolean performAccessibilityAction(android.view.View view, int action, android.os.Bundle arguments)
          Performs the specified accessibility action on the view.
static void postInvalidateOnAnimation(android.view.View view)
          Cause an invalidate to happen on the next animation time step, typically the next display frame.
static void postInvalidateOnAnimation(android.view.View view, int left, int top, int right, int bottom)
          Cause an invalidate of the specified area to happen on the next animation time step, typically the next display frame.
static void postOnAnimation(android.view.View view, Runnable action)
          Causes the Runnable to execute on the next animation time step.
static void postOnAnimationDelayed(android.view.View view, Runnable action, long delayMillis)
          Causes the Runnable to execute on the next animation time step, after the specified amount of time elapses.
static void setAccessibilityDelegate(android.view.View v, AccessibilityDelegateCompat delegate)
          Sets a delegate for implementing accessibility support via compositon as opposed to inheritance.
static void setHasTransientState(android.view.View view, boolean hasTransientState)
          Set whether this view is currently tracking transient state that the framework should attempt to preserve when possible.
static void setImportantForAccessibility(android.view.View view, int mode)
          Sets how to determine whether this view is important for accessibility which is if it fires accessibility events and if it is reported to accessibility services that query the screen.
static void setLabelFor(android.view.View view, int labeledId)
          Sets the id of a view for which a given view serves as a label for accessibility purposes.
static void setLayerPaint(android.view.View view, android.graphics.Paint paint)
          Updates the Paint object used with the current layer (used only if the current layer type is not set to LAYER_TYPE_NONE).
static void setLayerType(android.view.View view, int layerType, android.graphics.Paint paint)
          Specifies the type of layer backing this view.
static void setLayoutDirection(android.view.View view, int layoutDirection)
          Set the layout direction for this view.
static void setOverScrollMode(android.view.View v, int overScrollMode)
          Set the over-scroll mode for this view.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OVER_SCROLL_ALWAYS

public static final int OVER_SCROLL_ALWAYS
Always allow a user to over-scroll this view, provided it is a view that can scroll.

See Also:
Constant Field Values

OVER_SCROLL_IF_CONTENT_SCROLLS

public static final int OVER_SCROLL_IF_CONTENT_SCROLLS
Allow a user to over-scroll this view only if the content is large enough to meaningfully scroll, provided it is a view that can scroll.

See Also:
Constant Field Values

OVER_SCROLL_NEVER

public static final int OVER_SCROLL_NEVER
Never allow a user to over-scroll this view.

See Also:
Constant Field Values

IMPORTANT_FOR_ACCESSIBILITY_AUTO

public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO
Automatically determine whether a view is important for accessibility.

See Also:
Constant Field Values

IMPORTANT_FOR_ACCESSIBILITY_YES

public static final int IMPORTANT_FOR_ACCESSIBILITY_YES
The view is important for accessibility.

See Also:
Constant Field Values

IMPORTANT_FOR_ACCESSIBILITY_NO

public static final int IMPORTANT_FOR_ACCESSIBILITY_NO
The view is not important for accessibility.

See Also:
Constant Field Values

LAYER_TYPE_NONE

public static final int LAYER_TYPE_NONE
Indicates that the view does not have a layer.

See Also:
Constant Field Values

LAYER_TYPE_SOFTWARE

public static final int LAYER_TYPE_SOFTWARE

Indicates that the view has a software layer. A software layer is backed by a bitmap and causes the view to be rendered using Android's software rendering pipeline, even if hardware acceleration is enabled.

Software layers have various usages:

When the application is not using hardware acceleration, a software layer is useful to apply a specific color filter and/or blending mode and/or translucency to a view and all its children.

When the application is using hardware acceleration, a software layer is useful to render drawing primitives not supported by the hardware accelerated pipeline. It can also be used to cache a complex view tree into a texture and reduce the complexity of drawing operations. For instance, when animating a complex view tree with a translation, a software layer can be used to render the view tree only once.

Software layers should be avoided when the affected view tree updates often. Every update will require to re-render the software layer, which can potentially be slow (particularly when hardware acceleration is turned on since the layer will have to be uploaded into a hardware texture after every update.)

See Also:
Constant Field Values

LAYER_TYPE_HARDWARE

public static final int LAYER_TYPE_HARDWARE

Indicates that the view has a hardware layer. A hardware layer is backed by a hardware specific texture (generally Frame Buffer Objects or FBO on OpenGL hardware) and causes the view to be rendered using Android's hardware rendering pipeline, but only if hardware acceleration is turned on for the view hierarchy. When hardware acceleration is turned off, hardware layers behave exactly as software layers.

A hardware layer is useful to apply a specific color filter and/or blending mode and/or translucency to a view and all its children.

A hardware layer can be used to cache a complex view tree into a texture and reduce the complexity of drawing operations. For instance, when animating a complex view tree with a translation, a hardware layer can be used to render the view tree only once.

A hardware layer can also be used to increase the rendering quality when rotation transformations are applied on a view. It can also be used to prevent potential clipping issues when applying 3D transforms on a view.

See Also:
Constant Field Values

LAYOUT_DIRECTION_LTR

public static final int LAYOUT_DIRECTION_LTR
Horizontal layout direction of this view is from Left to Right.

See Also:
Constant Field Values

LAYOUT_DIRECTION_RTL

public static final int LAYOUT_DIRECTION_RTL
Horizontal layout direction of this view is from Right to Left.

See Also:
Constant Field Values

LAYOUT_DIRECTION_INHERIT

public static final int LAYOUT_DIRECTION_INHERIT
Horizontal layout direction of this view is inherited from its parent. Use with setLayoutDirection(android.view.View, int).

See Also:
Constant Field Values

LAYOUT_DIRECTION_LOCALE

public static final int LAYOUT_DIRECTION_LOCALE
Horizontal layout direction of this view is from deduced from the default language script for the locale. Use with setLayoutDirection(android.view.View, int).

See Also:
Constant Field Values
Constructor Detail

ViewCompat

public ViewCompat()
Method Detail

canScrollHorizontally

public static boolean canScrollHorizontally(android.view.View v,
                                            int direction)
Check if this view can be scrolled horizontally in a certain direction.

Parameters:
v - The View against which to invoke the method.
direction - Negative to check scrolling left, positive to check scrolling right.
Returns:
true if this view can be scrolled in the specified direction, false otherwise.

canScrollVertically

public static boolean canScrollVertically(android.view.View v,
                                          int direction)
Check if this view can be scrolled vertically in a certain direction.

Parameters:
v - The View against which to invoke the method.
direction - Negative to check scrolling up, positive to check scrolling down.
Returns:
true if this view can be scrolled in the specified direction, false otherwise.

getOverScrollMode

public static int getOverScrollMode(android.view.View v)
Returns the over-scroll mode for this view. The result will be one of OVER_SCROLL_ALWAYS (default), OVER_SCROLL_IF_CONTENT_SCROLLS (allow over-scrolling only if the view content is larger than the container), or OVER_SCROLL_NEVER.

Parameters:
v - The View against which to invoke the method.
Returns:
This view's over-scroll mode.

setOverScrollMode

public static void setOverScrollMode(android.view.View v,
                                     int overScrollMode)
Set the over-scroll mode for this view. Valid over-scroll modes are OVER_SCROLL_ALWAYS (default), OVER_SCROLL_IF_CONTENT_SCROLLS (allow over-scrolling only if the view content is larger than the container), or OVER_SCROLL_NEVER. Setting the over-scroll mode of a view will have an effect only if the view is capable of scrolling.

Parameters:
v - The View against which to invoke the method.
overScrollMode - The new over-scroll mode for this view.

onPopulateAccessibilityEvent

public static void onPopulateAccessibilityEvent(android.view.View v,
                                                android.view.accessibility.AccessibilityEvent event)
Called from View.dispatchPopulateAccessibilityEvent(AccessibilityEvent) giving a chance to this View to populate the accessibility event with its text content. While this method is free to modify event attributes other than text content, doing so should normally be performed in View.onInitializeAccessibilityEvent(AccessibilityEvent).

Example: Adding formatted date string to an accessibility event in addition to the text added by the super implementation:

 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
     super.onPopulateAccessibilityEvent(event);
     final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
     String selectedDateUtterance = DateUtils.formatDateTime(mContext,
         mCurrentDate.getTimeInMillis(), flags);
     event.getText().add(selectedDateUtterance);
 }

If an View.AccessibilityDelegate has been specified via calling View.setAccessibilityDelegate(android.view.View.AccessibilityDelegate) its View.AccessibilityDelegate.onPopulateAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters:
v - The View against which to invoke the method.
event - The accessibility event which to populate.
See Also:
View.sendAccessibilityEvent(int), View.dispatchPopulateAccessibilityEvent(AccessibilityEvent)

onInitializeAccessibilityEvent

public static void onInitializeAccessibilityEvent(android.view.View v,
                                                  android.view.accessibility.AccessibilityEvent event)
Initializes an AccessibilityEvent with information about this View which is the event source. In other words, the source of an accessibility event is the view whose state change triggered firing the event.

Example: Setting the password property of an event in addition to properties set by the super implementation:

 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     super.onInitializeAccessibilityEvent(event);
     event.setPassword(true);
 }

If an View.AccessibilityDelegate has been specified via calling View.setAccessibilityDelegate(android.view.View.AccessibilityDelegate) its View.AccessibilityDelegate.onInitializeAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters:
v - The View against which to invoke the method.
event - The event to initialize.
See Also:
View.sendAccessibilityEvent(int), View.dispatchPopulateAccessibilityEvent(AccessibilityEvent)

onInitializeAccessibilityNodeInfo

public static void onInitializeAccessibilityNodeInfo(android.view.View v,
                                                     AccessibilityNodeInfoCompat info)
Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:

Subclasses should override this method, call the super implementation, and set additional attributes.

If an View.AccessibilityDelegate has been specified via calling View.setAccessibilityDelegate(android.view.View.AccessibilityDelegate) its View.AccessibilityDelegate.onInitializeAccessibilityNodeInfo(View, android.view.accessibility.AccessibilityNodeInfo) is responsible for handling this call.

Parameters:
v - The View against which to invoke the method.
info - The instance to initialize.

setAccessibilityDelegate

public static void setAccessibilityDelegate(android.view.View v,
                                            AccessibilityDelegateCompat delegate)
Sets a delegate for implementing accessibility support via compositon as opposed to inheritance. The delegate's primary use is for implementing backwards compatible widgets. For more details see View.AccessibilityDelegate.

Parameters:
v - The View against which to invoke the method.
delegate - The delegate instance.
See Also:
View.AccessibilityDelegate

hasTransientState

public static boolean hasTransientState(android.view.View view)
Indicates whether the view is currently tracking transient state that the app should not need to concern itself with saving and restoring, but that the framework should take special note to preserve when possible.

Parameters:
view - View to check for transient state
Returns:
true if the view has transient state

setHasTransientState

public static void setHasTransientState(android.view.View view,
                                        boolean hasTransientState)
Set whether this view is currently tracking transient state that the framework should attempt to preserve when possible.

Parameters:
view - View tracking transient state
hasTransientState - true if this view has transient state

postInvalidateOnAnimation

public static void postInvalidateOnAnimation(android.view.View view)

Cause an invalidate to happen on the next animation time step, typically the next display frame.

This method can be invoked from outside of the UI thread only when this View is attached to a window.

Parameters:
view - View to invalidate

postInvalidateOnAnimation

public static void postInvalidateOnAnimation(android.view.View view,
                                             int left,
                                             int top,
                                             int right,
                                             int bottom)

Cause an invalidate of the specified area to happen on the next animation time step, typically the next display frame.

This method can be invoked from outside of the UI thread only when this View is attached to a window.

Parameters:
view - View to invalidate
left - The left coordinate of the rectangle to invalidate.
top - The top coordinate of the rectangle to invalidate.
right - The right coordinate of the rectangle to invalidate.
bottom - The bottom coordinate of the rectangle to invalidate.

postOnAnimation

public static void postOnAnimation(android.view.View view,
                                   Runnable action)

Causes the Runnable to execute on the next animation time step. The runnable will be run on the user interface thread.

This method can be invoked from outside of the UI thread only when this View is attached to a window.

Parameters:
view - View to post this Runnable to
action - The Runnable that will be executed.

postOnAnimationDelayed

public static void postOnAnimationDelayed(android.view.View view,
                                          Runnable action,
                                          long delayMillis)

Causes the Runnable to execute on the next animation time step, after the specified amount of time elapses. The runnable will be run on the user interface thread.

This method can be invoked from outside of the UI thread only when this View is attached to a window.

Parameters:
view - The view to post this Runnable to
action - The Runnable that will be executed.
delayMillis - The delay (in milliseconds) until the Runnable will be executed.

getImportantForAccessibility

public static int getImportantForAccessibility(android.view.View view)
Gets the mode for determining whether this View is important for accessibility which is if it fires accessibility events and if it is reported to accessibility services that query the screen.

Parameters:
view - The view whose property to get.
Returns:
The mode for determining whether a View is important for accessibility.
See Also:
IMPORTANT_FOR_ACCESSIBILITY_YES, IMPORTANT_FOR_ACCESSIBILITY_NO, IMPORTANT_FOR_ACCESSIBILITY_AUTO

setImportantForAccessibility

public static void setImportantForAccessibility(android.view.View view,
                                                int mode)
Sets how to determine whether this view is important for accessibility which is if it fires accessibility events and if it is reported to accessibility services that query the screen.

Parameters:
view - The view whose property to set.
mode - How to determine whether this view is important for accessibility.
See Also:
IMPORTANT_FOR_ACCESSIBILITY_YES, IMPORTANT_FOR_ACCESSIBILITY_NO, IMPORTANT_FOR_ACCESSIBILITY_AUTO

performAccessibilityAction

public static boolean performAccessibilityAction(android.view.View view,
                                                 int action,
                                                 android.os.Bundle arguments)
Performs the specified accessibility action on the view. For possible accessibility actions look at AccessibilityNodeInfoCompat.

If an AccessibilityDelegateCompat has been specified via calling setAccessibilityDelegate(View, AccessibilityDelegateCompat) its AccessibilityDelegateCompat.performAccessibilityAction(View, int, Bundle) is responsible for handling this call.

Parameters:
action - The action to perform.
arguments - Optional action arguments.
Returns:
Whether the action was performed.

getAccessibilityNodeProvider

public static AccessibilityNodeProviderCompat getAccessibilityNodeProvider(android.view.View view)
Gets the provider for managing a virtual view hierarchy rooted at this View and reported to AccessibilityServices that explore the window content.

If this method returns an instance, this instance is responsible for managing AccessibilityNodeInfoCompats describing the virtual sub-tree rooted at this View including the one representing the View itself. Similarly the returned instance is responsible for performing accessibility actions on any virtual view or the root view itself.

If an AccessibilityDelegateCompat has been specified via calling #setAccessibilityDelegate(View, AccessibilityDelegateCompat) its {@link AccessibilityDelegateCompat#getAccessibilityNodeProvider(View)} is responsible for handling this call.

Parameters:
view - The view whose property to get.
Returns:
The provider.
See Also:
AccessibilityNodeProviderCompat

setLayerType

public static void setLayerType(android.view.View view,
                                int layerType,
                                android.graphics.Paint paint)

Specifies the type of layer backing this view. The layer can be disabled, software or hardware.

A layer is associated with an optional Paint instance that controls how the layer is composed on screen. The following properties of the paint are taken into account when composing the layer:

If this view has an alpha value set to < 1.0 by calling setAlpha(float), the alpha value of the layer's paint is replaced by this view's alpha value. Calling setAlpha(float) is therefore equivalent to setting a hardware layer on this view and providing a paint with the desired alpha value.

Refer to the documentation of disabled, software and hardware for more information on when and how to use layers.

Parameters:
layerType - The ype of layer to use with this view, must be one of LAYER_TYPE_NONE, LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE
paint - The paint used to compose the layer. This argument is optional and can be null. It is ignored when the layer type is LAYER_TYPE_NONE
view - View to set the layer type for
layerType - The type of layer to use with this view, must be one of LAYER_TYPE_NONE, LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE
paint - The paint used to compose the layer. This argument is optional and can be null. It is ignored when the layer type is LAYER_TYPE_NONE

getLayerType

public static int getLayerType(android.view.View view)
Indicates what type of layer is currently associated with this view. By default a view does not have a layer, and the layer type is LAYER_TYPE_NONE. Refer to the documentation of setLayerType(android.view.View, int, android.graphics.Paint) for more information on the different types of layers.

Parameters:
view - The view to fetch the layer type from
Returns:
LAYER_TYPE_NONE, LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE
See Also:
setLayerType(android.view.View, int, android.graphics.Paint), LAYER_TYPE_NONE, LAYER_TYPE_SOFTWARE, LAYER_TYPE_HARDWARE

getLabelFor

public static int getLabelFor(android.view.View view)
Gets the id of a view for which a given view serves as a label for accessibility purposes.

Parameters:
view - The view on which to invoke the corresponding method.
Returns:
The labeled view id.

setLabelFor

public static void setLabelFor(android.view.View view,
                               int labeledId)
Sets the id of a view for which a given view serves as a label for accessibility purposes.

Parameters:
view - The view on which to invoke the corresponding method.
labeledId - The labeled view id.

setLayerPaint

public static void setLayerPaint(android.view.View view,
                                 android.graphics.Paint paint)
Updates the Paint object used with the current layer (used only if the current layer type is not set to LAYER_TYPE_NONE). Changed properties of the Paint provided to setLayerType(android.view.View, int, android.graphics.Paint) will be used the next time the View is redrawn, but setLayerPaint(android.view.View, android.graphics.Paint) must be called to ensure that the view gets redrawn immediately.

A layer is associated with an optional Paint instance that controls how the layer is composed on screen. The following properties of the paint are taken into account when composing the layer:

If this view has an alpha value set to < 1.0 by calling View#setAlpha(float), the alpha value of the layer's paint is replaced by this view's alpha value. Calling View#setAlpha(float) is therefore equivalent to setting a hardware layer on this view and providing a paint with the desired alpha value.

Parameters:
view - View to set a layer paint for
paint - The paint used to compose the layer. This argument is optional and can be null. It is ignored when the layer type is LAYER_TYPE_NONE
See Also:
setLayerType(View, int, android.graphics.Paint)

getLayoutDirection

public static int getLayoutDirection(android.view.View view)
Returns the resolved layout direction for this view.

Parameters:
view - View to get layout direction for
Returns:
LAYOUT_DIRECTION_RTL if the layout direction is RTL or returns LAYOUT_DIRECTION_LTR if the layout direction is not RTL. For compatibility, this will return LAYOUT_DIRECTION_LTR if API version is lower than Jellybean MR1 (API 17)

setLayoutDirection

public static void setLayoutDirection(android.view.View view,
                                      int layoutDirection)
Set the layout direction for this view. This will propagate a reset of layout direction resolution to the view's children and resolve layout direction for this view.

Parameters:
view - View to set layout direction for
layoutDirection - the layout direction to set. Should be one of: LAYOUT_DIRECTION_LTR, LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE. Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution proceeds up the parent chain of the view to get the value. If there is no parent, then it will return the default LAYOUT_DIRECTION_LTR.

getParentForAccessibility

public static android.view.ViewParent getParentForAccessibility(android.view.View view)
Gets the parent for accessibility purposes. Note that the parent for accessibility is not necessary the immediate parent. It is the first predecessor that is important for accessibility.

Parameters:
view - View to retrieve parent for
Returns:
The parent for use in accessibility inspection


Copyright © 2013 Marek Kedzierski. All Rights Reserved.