androidx.compose.foundation.content

Interfaces

ReceiveContentListener

A set of callbacks for contentReceiver modifier to get information about certain Drag-and-Drop state changes, as well as receiving the payload carrying TransferableContent.

Cmn

Classes

MediaType

Type identifier for contents that are transferable between applications or processes.

Cmn
android
PlatformTransferableContent

All the platform-specific information regarding a TransferableContent that cannot be abstracted away in a platform agnostic way.

Cmn
android
TransferableContent

Represents content that can be transferred between applications or processes.

Cmn
TransferableContent.Source

Defines the type of operation that a TransferableContent originates from.

Cmn

Modifiers

contentReceiver

Configures the current node and any children nodes as a Content Receiver.

Cmn

Extension functions summary

TransferableContent?

Helper function to consume parts of TransferableContent in Android by splitting it to ClipData.Item parts.

android
Boolean

Returns whether this TransferableContent can provide an item with the mediaType.

Cmn
android

Extension functions

TransferableContent.consume

@ExperimentalFoundationApi
fun TransferableContent.consume(predicate: (ClipData.Item) -> Boolean): TransferableContent?

Helper function to consume parts of TransferableContent in Android by splitting it to ClipData.Item parts. Use this function in contentReceiver modifier's onReceive callback to easily separate remaining parts from incoming TransferableContent.

import androidx.compose.foundation.Image
import androidx.compose.foundation.content.MediaType
import androidx.compose.foundation.content.consume
import androidx.compose.foundation.content.contentReceiver
import androidx.compose.foundation.content.hasMediaType
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap

val state = rememberTextFieldState()
var images by remember { mutableStateOf<List<ImageBitmap>>(emptyList()) }
Column {
    Row { images.forEach { Image(bitmap = it, contentDescription = null) } }
    BasicTextField(
        state = state,
        modifier =
            Modifier.contentReceiver { transferableContent ->
                if (!transferableContent.hasMediaType(MediaType.Image)) {
                    return@contentReceiver transferableContent
                }
                val newImages = mutableListOf<ImageBitmap>()
                transferableContent
                    .consume { item ->
                        // only consume this item if we can read an imageBitmap
                        item.readImageBitmap()?.let {
                            newImages += it
                            true
                        } ?: false
                    }
                    .also { images = newImages }
            },
    )
}
Parameters
predicate: (ClipData.Item) -> Boolean

Decides whether to consume or leave the given item out. Return true to indicate that this particular item was processed here, it shouldn't be passed further down the content receiver chain. Return false to keep it in the returned TransferableContent.

Returns
TransferableContent?

Remaining parts of this TransferableContent.

TransferableContent.hasMediaType

@ExperimentalFoundationApi
fun TransferableContent.hasMediaType(mediaType: MediaType): Boolean

Returns whether this TransferableContent can provide an item with the mediaType.