Search code examples
android-jetpack-composedisablecomposable

How can I disable a full view in Jetpack Compose


I want to disable a whole view from any interaction (e.g. button presses) when a Boolean in my view model is true. How can I do this in Jetpack Compose without having to disable each of the elements within the view?

See example below as to what I'm trying to do.

@Composable
fun MyView(alertViewModel: AlertViewModel = viewModel()) {

    var text by remember { mutableStateOf(TextFieldValue("")) }

    Column(
        /*

    Disable all elements in the column so I don't need to disable each element individually for example:
    
    modifier = Modifier
    .disabled(
    if (alertViewModel.showAlert == true) {
         true
    } else {
         false
    }

         */
    ) {

        Text(text = "My View")
        
        TextField(
            value = text,
            onValueChange = { newText ->
                text = newText
            }
        )

        Button(onClick = { /*TODO*/ }) {

        }
    }

}

Solution

  • Seems as though it isn't possible to disable a whole view in Jetpack Compose. All interact-able elements such as Button and TextField and have to be set to enabled = false individually.

    Buttons: Jetpack Compose: How to disable FloatingAction Button?

    TextFields: Jetpack Compose: Disable Interaction with TextField