TextContextMenuBuilderScope


Scope for building a text context menu in Modifier.addTextContextMenuComponents. See member functions for how to add context menu components to this scope as part of the Modifier.addTextContextMenuComponents modifier. The item function is not in the common source set, but is instead defined as an extension function in the platform specific source sets.

Summary

Public functions

Unit

Adds a separator to the list of text context menu components.

Cmn

Extension functions

Unit
TextContextMenuBuilderScope.item(
    key: Any,
    label: String,
    leadingIcon: @DrawableRes Int,
    onClick: TextContextMenuSession.() -> Unit
)

Adds an item to the list of text context menu components.

android

Public functions

separator

fun separator(): Unit

Adds a separator to the list of text context menu components. Successive separators will be combined into a single separator.

Extension functions

fun TextContextMenuBuilderScope.item(
    key: Any,
    label: String,
    leadingIcon: @DrawableRes Int = Resources.ID_NULL,
    onClick: TextContextMenuSession.() -> Unit
): Unit

Adds an item to the list of text context menu components.

import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.contextmenu.builder.item
import androidx.compose.foundation.text.contextmenu.modifier.addTextContextMenuComponents
import androidx.compose.foundation.text.input.clearText
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource

val textFieldState = rememberTextFieldState()
val label = stringResource(R.string.context_menu_clear)
BasicTextField(
    state = textFieldState,
    modifier =
        Modifier.addTextContextMenuComponents {
            separator()
            item(
                key = ClearKeyDataObject,
                label = label,
                leadingIcon = R.drawable.ic_sample_vector,
            ) {
                textFieldState.clearText()
                close()
            }
            separator()
        },
)
Parameters
key: Any

A unique key that identifies this item. Used to identify context menu items in the context menu. It is advisable to use a data object as a key here.

label: String

string to display as the text of the item.

leadingIcon: @DrawableRes Int = Resources.ID_NULL

Icon that precedes the label in the context menu. This is expected to be a drawable resource reference. Setting this to the default value Resources.ID_NULL means that it will not be displayed.

onClick: TextContextMenuSession.() -> Unit

Action to perform upon the item being clicked/pressed.