Android Compose – Disable Button
In this tutorial, we will learn how to disable a Button in Android Compose.
To make the Button disabled in Android Jetpack Compose, set enabled parameter to false.
Button(enabled = false)
Example
In this example, we have UI with a Button. The button has the text Submit
. And we disable this Button using enabled parameter.
Create a Project in Android Studio with empty compose activity template, and modify MainActivity.kt file as shown in the following.
MainActivity.kt
</>
Copy
package com.example.myapplication
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.padding(20.dp)
.fillMaxWidth()) {
val context = LocalContext.current
Button(
enabled = false,
onClick = {}
) {
Text("Submit")
}
}
}
}
}
}
Screenshot
The button turns to grey, and looks like an inactive button.
Conclusion
In this Android Jetpack Compose Tutorial, we learned how to disable a Button using enable parameter.