{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

ModifierNodeElement

public abstract class ModifierNodeElement<N extends Modifier.Node> implements Modifier.Element, InspectableValue


A Modifier.Element which manages an instance of a particular Modifier.Node implementation. A given Modifier.Node implementation can only be used when a ModifierNodeElement which creates and updates that implementation is applied to a Layout.

A ModifierNodeElement should be very lightweight, and do little more than hold the information necessary to create and maintain an instance of the associated Modifier.Node type.

import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.node.DrawModifierNode
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.platform.InspectorInfo

class Circle(var color: Color) : DrawModifierNode, Modifier.Node() {
    override fun ContentDrawScope.draw() {
        drawCircle(color)
    }
}
data class CircleElement(
    val color: Color
) : ModifierNodeElement<Circle>() {
    override fun create() = Circle(color)
    override fun update(node: Circle) {
        node.color = color
    }
    override fun InspectorInfo.inspectableProperties() {
        name = "circle"
        properties["color"] = color
    }
}
fun Modifier.circle(color: Color) = this then CircleElement(color)
import androidx.compose.ui.Modifier
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.node.SemanticsModifierNode
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.semantics.SemanticsPropertyReceiver
import androidx.compose.ui.semantics.heading

class HeadingNode : SemanticsModifierNode, Modifier.Node() {
    override fun SemanticsPropertyReceiver.applySemantics() {
        heading()
    }
}

val HeadingElement = object : ModifierNodeElement<HeadingNode>() {
    override fun create() = HeadingNode()

    override fun update(node: HeadingNode) {
        // Nothing to update.
    }

    override fun InspectorInfo.inspectableProperties() {
        name = "heading"
    }

    override fun hashCode(): Int = "heading".hashCode()
    override fun equals(other: Any?) = (other === this)
}

fun Modifier.heading() = this then HeadingElement
See also
Modifier.Node
Modifier.Element

Summary

Public constructors

Public methods

abstract @NonNull N

This will be called the first time the modifier is applied to the Layout and it should construct and return the corresponding Modifier.Node instance.

abstract boolean
equals(Object other)

Require equals() to be implemented.

final @NonNull Sequence<@NonNull ValueElement>

The elements of a compose value.

final String

Use this name as the reference name shown in tools of this value if there is no explicit reference name given to the value.

final Object

Use this value as a readable representation of the value.

abstract int

Require hashCode() to be implemented.

void

Populates an InspectorInfo object with attributes to display in the layout inspector.

abstract void
update(@NonNull N node)

Called when a modifier is applied to a Layout whose inputs have changed from the previous application.

Inherited methods

From androidx.compose.ui.Modifier
@NonNull Modifier

Concatenates this modifier with another.

From androidx.compose.ui.Modifier.Element
boolean
all(
    @NonNull Function1<@NonNull Modifier.Element, @NonNull Boolean> predicate
)

Returns true if predicate returns true for all Elements in this Modifier or if this Modifier contains no Elements.

boolean
any(
    @NonNull Function1<@NonNull Modifier.Element, @NonNull Boolean> predicate
)

Returns true if predicate returns true for any Element in this Modifier.

@NonNull R
<R extends Object> foldIn(
    @NonNull R initial,
    @NonNull Function2<@NonNull R, @NonNull Modifier.Element, @NonNull R> operation
)

Accumulates a value starting with initial and applying operation to the current value and each element from outside in.

@NonNull R
<R extends Object> foldOut(
    @NonNull R initial,
    @NonNull Function2<@NonNull Modifier.Element, @NonNull R, @NonNull R> operation
)

Accumulates a value starting with initial and applying operation to the current value and each element from inside out.

Public constructors

ModifierNodeElement

public <N extends Modifier.Node> ModifierNodeElement()

Public methods

create

public abstract @NonNullcreate()

This will be called the first time the modifier is applied to the Layout and it should construct and return the corresponding Modifier.Node instance.

equals

public abstract boolean equals(Object other)

Require equals() to be implemented. Using a data class is sufficient. Singletons may implement this function with referential equality (this === other). Modifiers with no inputs may implement this function by checking the type of the other object.

getInspectableElements

public final @NonNull Sequence<@NonNull ValueElementgetInspectableElements()

The elements of a compose value.

getNameFallback

public final String getNameFallback()

Use this name as the reference name shown in tools of this value if there is no explicit reference name given to the value. Example: a modifier in a modifier list.

getValueOverride

public final Object getValueOverride()

Use this value as a readable representation of the value.

hashCode

public abstract int hashCode()

Require hashCode() to be implemented. Using a data class is sufficient. Singletons and modifiers with no parameters may implement this function by returning an arbitrary constant.

inspectableProperties

public void inspectableProperties(@NonNull InspectorInfo receiver)

Populates an InspectorInfo object with attributes to display in the layout inspector. This is called by tooling to resolve the properties of this modifier. By convention, implementors should set the name to the function name of the modifier.

The default implementation will attempt to reflectively populate the inspector info with the properties declared on the subclass. It will also set the name property to the name of this instance's class by default (not the name of the modifier function). Modifier property population depends on the kotlin-reflect library. If it is not in the classpath at runtime, the default implementation of this function will populate the properties with an error message.

If you override this function and provide the properties you wish to display, you do not need to call super. Doing so may result in duplicate properties appearing in the layout inspector.

update

public abstract void update(@NonNull N node)

Called when a modifier is applied to a Layout whose inputs have changed from the previous application. This function will have the current node instance passed in as a parameter, and it is expected that the node will be brought up to date.