ClipEntry


Platform specific protocol that expresses an item in the native Clipboard.

Summary

Public companion functions

ClipEntry
withText(plainText: String, html: String?)

Creates an entry with plainText and optional html representations.

Cmn
android
ClipEntry
withUrl(url: String, plainText: String, html: String?)

Creates an entry with URL, plain-text and optional HTML representation.

Cmn
android

Public constructors

ClipEntry(clipData: ClipData)
android

Public functions

suspend String?

Reads the HTML representation.

Cmn
android
suspend String?

Reads the plain-text representation.

Cmn
android
suspend String?

Reads the first available text representation of this clip.

Cmn
android
suspend String?

Reads the URL representation.

Cmn
android

Public properties

ClipData
android
ClipMetadata

Returns a ClipMetadata which describes the contents of this ClipEntry.

Cmn
android

Extension functions

Uri?

Returns the first non-null Uri from the list of ClipData.Items in this ClipEntry.

android

Public companion functions

withText

fun withText(plainText: String, html: String? = null): ClipEntry

Creates an entry with plainText and optional html representations.

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.ClipEntry
import androidx.compose.ui.platform.LocalClipboard

val clipboard = LocalClipboard.current
val coroutineScope = rememberCoroutineScope()

var copyCounter by remember { mutableIntStateOf(0) }
val textToCopy = "Copyable Text $copyCounter"

Column {
    Text("Text to copy: $textToCopy")
    Button(
        onClick = {
            val clipEntry = ClipEntry.withText(plainText = textToCopy)
            coroutineScope.launch {
                clipboard.setClipEntry(clipEntry)
            }
            copyCounter++
        }
    ) {
        Text("Copy")
    }
}
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.ClipEntry
import androidx.compose.ui.platform.LocalClipboard
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle

val clipboard = LocalClipboard.current
val coroutineScope = rememberCoroutineScope()

var copyCounter by remember { mutableIntStateOf(0) }
val textToCopy = buildAnnotatedString {
    append("Copy counter = ")
    withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
        append("$copyCounter")
    }
}

Column {
    Text(textToCopy)
    Button(
        onClick = {
            val clipEntry =
                ClipEntry.withText(
                    plainText = textToCopy.text,
                    html = "Copy counter = <b>$copyCounter</b>",
                )
            coroutineScope.launch {
                clipboard.setClipEntry(clipEntry)
            }
            copyCounter++
        }
    ) {
        Text("Copy")
    }
}
Parameters
plainText: String

plain-text representation of the item

html: String? = null

optional HTML representation of the same text content

withUrl

fun withUrl(url: String, plainText: String = url, html: String? = null): ClipEntry

Creates an entry with URL, plain-text and optional HTML representation.

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.ClipEntry
import androidx.compose.ui.platform.LocalClipboard

val clipboard = LocalClipboard.current
val coroutineScope = rememberCoroutineScope()
val url = "https://developer.android.com/jetpack/compose"
val linkText = "Jetpack Compose"
val linkHtml = "<a href='$url'>$linkText</a>"

Column {
    Text("Link to copy: $linkText")
    Button(
        onClick = {
            val clipEntry = ClipEntry.withUrl(url = url, plainText = linkText, html = linkHtml)
            coroutineScope.launch {
                clipboard.setClipEntry(clipEntry)
            }
        }
    ) {
        Text("Copy link")
    }
}
Parameters
url: String

valid URL string to write; this function does not validate url

plainText: String = url

plain-text fallback, such as a label; defaults to url

html: String? = null

optional HTML link for the same URL

Public constructors

ClipEntry

ClipEntry(clipData: ClipData)

Public functions

readHtml

suspend fun readHtml(): String?

Reads the HTML representation. If there is no HTML representation available, this method doesn't try to read other text values.

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.ClipEntry
import androidx.compose.ui.platform.LocalClipboard

val clipboard = LocalClipboard.current
val coroutineScope = rememberCoroutineScope()
var allRepresentationsText by remember { mutableStateOf("") }

Column {
    Text("Clipboard content: '$allRepresentationsText'")

    Button(
        onClick = {
            coroutineScope.launch {
                val clipEntry = clipboard.getClipEntry() ?: return@launch

                allRepresentationsText = buildString {
                    append("Plain text = ")
                    if (clipEntry.clipMetadata.hasPlainText()) {
                        // For example: `compose_logo.png`
                        append(clipEntry.readPlainText() ?: "null")
                    } else {
                        append("N/A")
                    }
                    append("\n")

                    append("HTML = ")
                    if (clipEntry.clipMetadata.hasHtml()) {
                        // For example: `<a href='http://localhost:8080/compose_logo.png'></a>`
                        append(clipEntry.readHtml() ?: "null")
                    } else {
                        append("N/A")
                    }
                    append("\n")

                    append("URL = ")
                    if (clipEntry.clipMetadata.hasUrl()) {
                        // For example: `http://localhost:8080/compose_logo.png`
                        append(clipEntry.readUrl() ?: "null")
                    } else {
                        append("N/A")
                    }
                }
            }
        }
    ) {
        Text("Read Clipboard")
    }
}
Returns
String?

HTML markup, or null if unavailable or reading fails

readPlainText

suspend fun readPlainText(): String?

Reads the plain-text representation.

Unlike readText, returns null when plain text is unavailable instead of trying other representations.

Use readPlainText() when the caller accepts only plain text and should get null rather than HTML or a URL if plain text is unavailable.

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.ClipEntry
import androidx.compose.ui.platform.LocalClipboard

val clipboard = LocalClipboard.current
val coroutineScope = rememberCoroutineScope()
var allRepresentationsText by remember { mutableStateOf("") }

Column {
    Text("Clipboard content: '$allRepresentationsText'")

    Button(
        onClick = {
            coroutineScope.launch {
                val clipEntry = clipboard.getClipEntry() ?: return@launch

                allRepresentationsText = buildString {
                    append("Plain text = ")
                    if (clipEntry.clipMetadata.hasPlainText()) {
                        // For example: `compose_logo.png`
                        append(clipEntry.readPlainText() ?: "null")
                    } else {
                        append("N/A")
                    }
                    append("\n")

                    append("HTML = ")
                    if (clipEntry.clipMetadata.hasHtml()) {
                        // For example: `<a href='http://localhost:8080/compose_logo.png'></a>`
                        append(clipEntry.readHtml() ?: "null")
                    } else {
                        append("N/A")
                    }
                    append("\n")

                    append("URL = ")
                    if (clipEntry.clipMetadata.hasUrl()) {
                        // For example: `http://localhost:8080/compose_logo.png`
                        append(clipEntry.readUrl() ?: "null")
                    } else {
                        append("N/A")
                    }
                }
            }
        }
    ) {
        Text("Read Clipboard")
    }
}
Returns
String?

plain text, or null if unavailable or reading fails

readText

suspend fun readText(): String?

Reads the first available text representation of this clip.

Use readText() when you want the best-effort text value from a clip: it prefers plain text, then falls back to HTML or a URL. Use readPlainText, readHtml, or readUrl to request a specific representation.

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.ClipEntry
import androidx.compose.ui.platform.LocalClipboard

val clipboard = LocalClipboard.current
val coroutineScope = rememberCoroutineScope()
var textFromClipboard by remember { mutableStateOf("") }

Column {
    Text("Text from Clipboard: '$textFromClipboard'")

    Button(
        onClick = {
            coroutineScope.launch {
                val clipEntry = clipboard.getClipEntry() ?: return@launch
                // First, check if the entry contains a text representation: plain text, HTML or
                // URL
                if (clipEntry.clipMetadata.hasText()) {
                    // readText() value can be either the plain text, HTML, URL string or null
                    textFromClipboard = clipEntry.readText() ?: "null"
                }
            }
        }
    ) {
        Text("Read Clipboard")
    }
}
Returns
String?

plain text, HTML markup, or URL text, or null if none can be read

readUrl

suspend fun readUrl(): String?

Reads the URL representation. If there is no URL representation available, this method doesn't try to read other text values.

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.ClipEntry
import androidx.compose.ui.platform.LocalClipboard

val clipboard = LocalClipboard.current
val coroutineScope = rememberCoroutineScope()
var allRepresentationsText by remember { mutableStateOf("") }

Column {
    Text("Clipboard content: '$allRepresentationsText'")

    Button(
        onClick = {
            coroutineScope.launch {
                val clipEntry = clipboard.getClipEntry() ?: return@launch

                allRepresentationsText = buildString {
                    append("Plain text = ")
                    if (clipEntry.clipMetadata.hasPlainText()) {
                        // For example: `compose_logo.png`
                        append(clipEntry.readPlainText() ?: "null")
                    } else {
                        append("N/A")
                    }
                    append("\n")

                    append("HTML = ")
                    if (clipEntry.clipMetadata.hasHtml()) {
                        // For example: `<a href='http://localhost:8080/compose_logo.png'></a>`
                        append(clipEntry.readHtml() ?: "null")
                    } else {
                        append("N/A")
                    }
                    append("\n")

                    append("URL = ")
                    if (clipEntry.clipMetadata.hasUrl()) {
                        // For example: `http://localhost:8080/compose_logo.png`
                        append(clipEntry.readUrl() ?: "null")
                    } else {
                        append("N/A")
                    }
                }
            }
        }
    ) {
        Text("Read Clipboard")
    }
}
Returns
String?

URL string, or null if unavailable or reading fails

Public properties

clipData

val clipData: ClipData

clipMetadata

val clipMetadata: ClipMetadata

Returns a ClipMetadata which describes the contents of this ClipEntry. This is an ideal way to check whether to accept or reject what may be pasted from the clipboard without explicitly reading the content.

Calling this function does not trigger any content access warnings on any platform.

Extension functions

ClipEntry.firstUriOrNull

@ExperimentalComposeUiApi
fun ClipEntry.firstUriOrNull(): Uri?

Returns the first non-null Uri from the list of ClipData.Items in this ClipEntry.

ClipEntry can contain single or multiple ClipData.Items. This function is useful when you are only interested in processing just a single Uri item inside the ClipEntry.

It's advised that you consider checking all the items inside ClipEntry.clipData to thoroughly process a given ClipEntry.