I'm trying to find a simple solution on how to disable copy/paste/cut in a TextField. I did come across a couple of question but no answer.
3 Answers
Create an empty toolbar:
object EmptyTextToolbar: TextToolbar {
override val status: TextToolbarStatus = TextToolbarStatus.Hidden
override fun hide() { }
override fun showMenu(
rect: Rect,
onCopyRequested: (() -> Unit)?,
onPasteRequested: (() -> Unit)?,
onCutRequested: (() -> Unit)?,
onSelectAllRequested: (() -> Unit)?,
) {
}
}
Then you can provide it using LocalTextToolbar
.
Most probably you also don't need text selection in this case, here's how you can disable it too:
var textValue by remember { mutableStateOf(TextFieldValue("")) }
CompositionLocalProvider(
LocalTextToolbar provides EmptyTextToolbar
) {
TextField(
value = textValue,
onValueChange = { newValue ->
textValue = if (newValue.selection.length > 0) {
newValue.copy(selection = textValue.selection)
} else {
newValue
}
}
)
}

- 67,741
- 15
- 184
- 220
-
4Bit overcomplicated isn't it? – Richard Onslow Roper Apr 30 '22 at 07:41
-
after keeping this code,still i am able to select text and from the keyboard backspace able to remove the text – Mohd Qasim Feb 27 '23 at 09:11
-
@MohdQasim you shouldn't be able to select multiple characters if you use my code from `onValueChange`. But this code doesn't expect to block backspace, so you still can remove characters one by one – Phil Dukhov Feb 27 '23 at 10:35
For now, you could just add a dummy Transparent Composable on TOP of the TextField
and hook up it's click listener with the TextField's focusRequestor
.
val tffr = remember { FocusRequestor() } // TextField Focus-Requestor
Box{
TextField(
value = value,
onValueChange = { value = it }
modifier = Modifier.focusRequestor(tffr).focusable()
)
Surface(
modifier = Modifier.clickable { tffr.requestFocus() }.alpha(0)
)
}
This way, when users try to click on the TextField
, the tap would be intercepted by our dummy Composable, which would transfer the focus, effectively, to the TextField.
This means that they will technically be allowed to tap on the TextField
and type in it, but the long press won't be registered by the field for them.
You could even add a Long-Click-Listener to the Surface using the combinedClick
modifier, and display a toast to they users stating that the copy/paste actions are disabled, if that is something you'd require, that is.

- 5,477
- 2
- 11
- 42
-
2A possible solution, but it also blocks clicks, which may be useful for moving the pointer – Phil Dukhov Apr 30 '22 at 09:06
-
Makes sense Philip. Not a biggie for small value-holders though... – Richard Onslow Roper Apr 30 '22 at 09:18
Try this answer it's disable cut, copy in textfield
etSearchData.customSelectionActionModeCallback = object : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
}
}
where etSearchData is your textField ID

- 42
- 3