GVG
GVG

Reputation: 609

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*/ }) {

        }
    }

}

Upvotes: 4

Views: 3469

Answers (2)

Adnan Bashir
Adnan Bashir

Reputation: 763

comment or remove enableEdgeToEdge for stop full screen in jetpack compose

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
     //  enableEdgeToEdge()
        setContent { } 

Upvotes: 0

GVG
GVG

Reputation: 609

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

Upvotes: 3

Related Questions