Is there a way to hide the selection handle that a...
# compose
o
Is there a way to hide the selection handle that appears on a
TextField
? I basically want the
TextField
to be just for input and without selection, magnifier, copy or paste.
s
I don’t know if it does even remotely what you’re asking for, but I’d definitely start looking at BasicTextField if you want to so heavily alter what the “normal”
TextField
would normally do. It most likely gives you more control to achieve what you want over the normal
TextField
o
I am using
BasicTextField
, but that doesn’t give me more control. I even tried to wrap it in a
DisableSelection
but that apparently only works for
Text
.
s
Yeah that’s unfortunate. Have you the considered taking a look at CoreTextField instead, and copying to your project only the parts that you want to use? Again, not sure this is your solution, but that’s what I’d try next
Also maybe something to make as a feature request to google’s issuetracker, since if this is in fact your solution maybe they should expose those classes instead of having them be internal. Or at least provide some APIs that allow this functionality to be altered. If there is a use case there you know. Since disabling copy paste and stuff like that may be something they want to discourage you from disabling in the first place.
a
Hi, there. you can do smth like this that also works for BasicTextField and set the handle color to Color.Transparent to make it visually disappear: https://twitter.com/astamatok/status/1575475946546552833
Additionally if I understood correctly what you want is to remove the toolbar options completely it seems, for which you can do smth like this which removes the toolbar of options on selection, code might change slightly depending con your compose version:
Copy code
CompositionLocalProvider(
    LocalTextToolbar provides CustomTextToolbar()
) {
    var text by remember { mutableStateOf("") }
    BasicTextField(value = text, onValueChange = { text = it })
}
Copy code
import androidx.compose.ui.platform.TextToolbar
import androidx.compose.ui.platform.TextToolbarStatus
class CustomTextToolbar : TextToolbar {
    override fun hide() {}

    override val status: TextToolbarStatus = TextToolbarStatus.Hidden

    override fun showMenu(
        rect: androidx.compose.ui.geometry.Rect,
        onCopyRequested: (() -> Unit)?,
        onPasteRequested: (() -> Unit)?,
        onCutRequested: (() -> Unit)?,
        onSelectAllRequested: (() -> Unit)?
    ) {
    }
}
Hope this helps
o
Thanks. I was already using something similar to hide the
TextToolbar
. Hiding the handle by making the color transparent works, but it also makes the cursor disappear on first focus.
a
Oh really? thats odd, cursorColor has its own color, i tested and didnt find this behaviour, you could share a min repro?
o
I am unfortunately still on compose 1.1.0. Maybe that is related. The cursor is only invisible on first click. A second click makes it visible.
245 Views