ViewKt

Added in 1.1.0

public final class ViewKt


Summary

Public methods

static final void
doOnAttach(
    @NonNull View receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action when this view is attached to a window.

static final void
doOnDetach(
    @NonNull View receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action when this view is detached from a window.

static final void
doOnLayout(
    @NonNull View receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action when this view is laid out.

static final void
doOnNextLayout(
    @NonNull View receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action when this view is next laid out.

static final @NonNull OneShotPreDrawListener
doOnPreDraw(
    @NonNull View receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action when the view tree is about to be drawn.

static final @NonNull Bitmap

Return a Bitmap representation of this View.

static final @NonNull Sequence<@NonNull View>

Returns a Sequence over this view and its descendants recursively.

static final @NonNull Sequence<@NonNull ViewParent>

Returns a Sequence of the parent chain of this view by repeatedly calling View.getParent.

static final int

Returns the bottom margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

static final int

Returns the end margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

static final int

Returns the left margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

static final int

Returns the right margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

static final int

Returns the start margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

static final int

Returns the top margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

static final boolean
isGone(@NonNull View receiver)

Returns true when this view's visibility is View.GONE, false otherwise.

static final boolean

Returns true when this view's visibility is View.INVISIBLE, false otherwise.

static final boolean
isVisible(@NonNull View receiver)

Returns true when this view's visibility is View.VISIBLE, false otherwise.

static final @NonNull Runnable
postDelayed(
    @NonNull View receiver,
    long delayInMillis,
    @NonNull Function0<Unit> action
)

Version of View.postDelayed which re-orders the parameters, allowing the action to be placed outside of parentheses.

static final @NonNull Runnable
postOnAnimationDelayed(
    @NonNull View receiver,
    long delayInMillis,
    @NonNull Function0<Unit> action
)

Version of View.postOnAnimationDelayed which re-orders the parameters, allowing the action to be placed outside of parentheses.

static final void
setGone(@NonNull View receiver, boolean isGone)

Returns true when this view's visibility is View.GONE, false otherwise.

static final void
setInvisible(@NonNull View receiver, boolean isInvisible)

Returns true when this view's visibility is View.INVISIBLE, false otherwise.

static final void
setPadding(@NonNull View receiver, @Px int size)

Sets the view's padding.

static final void
setVisible(@NonNull View receiver, boolean isVisible)

Returns true when this view's visibility is View.VISIBLE, false otherwise.

static final void

Executes block with the View's layoutParams and reassigns the layoutParams with the updated version.

static final void
<T extends ViewGroup.LayoutParams> updateLayoutParamsTyped(
    @NonNull View receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull T, Unit> block
)

Executes block with a typed version of the View's layoutParams and reassigns the layoutParams with the updated version.

static final void
updatePadding(
    @NonNull View receiver,
    @Px int left,
    @Px int top,
    @Px int right,
    @Px int bottom
)

Updates this view's padding.

static final void
updatePaddingRelative(
    @NonNull View receiver,
    @Px int start,
    @Px int top,
    @Px int end,
    @Px int bottom
)

Updates this view's relative padding.

Public methods

doOnAttach

public static final void doOnAttach(
    @NonNull View receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action when this view is attached to a window. If the view is already attached to a window the action will be performed immediately, otherwise the action will be performed after the view is next attached.

The action will only be invoked once, and any listeners will then be removed.

See also
doOnDetach

doOnDetach

public static final void doOnDetach(
    @NonNull View receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action when this view is detached from a window. If the view is not attached to a window the action will be performed immediately, otherwise the action will be performed after the view is detached from its current window.

The action will only be invoked once, and any listeners will then be removed.

See also
doOnAttach

doOnLayout

public static final void doOnLayout(
    @NonNull View receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action when this view is laid out. If the view has been laid out and it has not requested a layout, the action will be performed straight away, otherwise the action will be performed after the view is next laid out.

The action will only be invoked once on the next layout and then removed.

See also
doOnNextLayout

doOnNextLayout

public static final void doOnNextLayout(
    @NonNull View receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action when this view is next laid out.

The action will only be invoked once on the next layout and then removed.

See also
doOnLayout

doOnPreDraw

public static final @NonNull OneShotPreDrawListener doOnPreDraw(
    @NonNull View receiver,
    @NonNull Function1<@NonNull ViewUnit> action
)

Performs the given action when the view tree is about to be drawn.

The action will only be invoked once prior to the next draw and then removed.

drawToBitmap

public static final @NonNull Bitmap drawToBitmap(@NonNull View receiver, @NonNull Bitmap.Config config)

Return a Bitmap representation of this View.

The resulting bitmap will be the same width and height as this view's current layout dimensions. This does not take into account any transformations such as scale or translation.

Note, this will use the software rendering pipeline to draw the view to the bitmap. This may result with different drawing to what is rendered on a hardware accelerated canvas (such as the device screen).

If this view has not been laid out this method will throw a IllegalStateException.

Parameters
@NonNull Bitmap.Config config

Bitmap config of the desired bitmap. Defaults to Bitmap.Config.ARGB_8888.

getAllViews

public static final @NonNull Sequence<@NonNull ViewgetAllViews(@NonNull View receiver)

Returns a Sequence over this view and its descendants recursively. This is a depth-first traversal similar to View.findViewById. A view with no children will return a single-element sequence of itself.

See also
descendants

getAncestors

public static final @NonNull Sequence<@NonNull ViewParentgetAncestors(@NonNull View receiver)

Returns a Sequence of the parent chain of this view by repeatedly calling View.getParent. An unattached view will return a zero-element sequence.

See also
descendants

getMarginBottom

public static final int getMarginBottom(@NonNull View receiver)

Returns the bottom margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

getMarginEnd

public static final int getMarginEnd(@NonNull View receiver)

Returns the end margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

getMarginLeft

public static final int getMarginLeft(@NonNull View receiver)

Returns the left margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

getMarginRight

public static final int getMarginRight(@NonNull View receiver)

Returns the right margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

getMarginStart

public static final int getMarginStart(@NonNull View receiver)

Returns the start margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

getMarginTop

public static final int getMarginTop(@NonNull View receiver)

Returns the top margin if this view's ViewGroup.LayoutParams is a ViewGroup.MarginLayoutParams, otherwise 0.

isGone

public static final boolean isGone(@NonNull View receiver)

Returns true when this view's visibility is View.GONE, false otherwise.

if (view.isGone) {
// Behavior...
}

Setting this property to true sets the visibility to View.GONE, false to View.VISIBLE.

view.isGone = true

isInvisible

public static final boolean isInvisible(@NonNull View receiver)

Returns true when this view's visibility is View.INVISIBLE, false otherwise.

if (view.isInvisible) {
// Behavior...
}

Setting this property to true sets the visibility to View.INVISIBLE, false to View.VISIBLE.

view.isInvisible = true

isVisible

public static final boolean isVisible(@NonNull View receiver)

Returns true when this view's visibility is View.VISIBLE, false otherwise.

if (view.isVisible) {
// Behavior...
}

Setting this property to true sets the visibility to View.VISIBLE, false to View.GONE.

view.isVisible = true

postDelayed

public static final @NonNull Runnable postDelayed(
    @NonNull View receiver,
    long delayInMillis,
    @NonNull Function0<Unit> action
)

Version of View.postDelayed which re-orders the parameters, allowing the action to be placed outside of parentheses.

view.postDelayed(200) {
doSomething()
}
Returns
@NonNull Runnable

the created Runnable

postOnAnimationDelayed

public static final @NonNull Runnable postOnAnimationDelayed(
    @NonNull View receiver,
    long delayInMillis,
    @NonNull Function0<Unit> action
)

Version of View.postOnAnimationDelayed which re-orders the parameters, allowing the action to be placed outside of parentheses.

view.postOnAnimationDelayed(16) {
doSomething()
}
Returns
@NonNull Runnable

the created Runnable

setGone

public static final void setGone(@NonNull View receiver, boolean isGone)

Returns true when this view's visibility is View.GONE, false otherwise.

if (view.isGone) {
// Behavior...
}

Setting this property to true sets the visibility to View.GONE, false to View.VISIBLE.

view.isGone = true

setInvisible

public static final void setInvisible(@NonNull View receiver, boolean isInvisible)

Returns true when this view's visibility is View.INVISIBLE, false otherwise.

if (view.isInvisible) {
// Behavior...
}

Setting this property to true sets the visibility to View.INVISIBLE, false to View.VISIBLE.

view.isInvisible = true

setPadding

public static final void setPadding(@NonNull View receiver, @Px int size)

Sets the view's padding. This version of the method sets all axes to the provided size.

See also
setPadding

setVisible

public static final void setVisible(@NonNull View receiver, boolean isVisible)

Returns true when this view's visibility is View.VISIBLE, false otherwise.

if (view.isVisible) {
// Behavior...
}

Setting this property to true sets the visibility to View.VISIBLE, false to View.GONE.

view.isVisible = true

updateLayoutParams

public static final void updateLayoutParams(
    @NonNull View receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull ViewGroup.LayoutParamsUnit> block
)

Executes block with the View's layoutParams and reassigns the layoutParams with the updated version.

updateLayoutParamsTyped

public static final void <T extends ViewGroup.LayoutParams> updateLayoutParamsTyped(
    @NonNull View receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull T, Unit> block
)

Executes block with a typed version of the View's layoutParams and reassigns the layoutParams with the updated version.

updatePadding

public static final void updatePadding(
    @NonNull View receiver,
    @Px int left,
    @Px int top,
    @Px int right,
    @Px int bottom
)

Updates this view's padding. This version of the method allows using named parameters to just set one or more axes.

See also
setPadding

updatePaddingRelative

public static final void updatePaddingRelative(
    @NonNull View receiver,
    @Px int start,
    @Px int top,
    @Px int end,
    @Px int bottom
)

Updates this view's relative padding. This version of the method allows using named parameters to just set one or more axes.

Note that this inline method references platform APIs added in API 17 and may raise runtime verification warnings on earlier platforms. See Chromium's guide to Class Verification Failures for more information.