Button in Android using Jetpack Compose
Jetpack Compose is a new toolkit provided by Google. This is useful for designing beautiful UI designs. A Button is a UI component in Android which is used to navigate between different screens. With the help of a button, the user can interact with your app and perform multiple actions inside your application. In this article, we will take a look at the implementation of buttons in Android using Jetpack Compose.
Attributes of the Button Widget
Attribute | Description |
---|---|
onClick | To perform an action when your button is clicked by the user. |
modifier | this parameter is used to add padding to our button. |
enabled | to enable or disable your button. |
border | this parameter is used to add a border stroke to our button. |
shape | this parameter is used to add shape to our button. |
Text() | this parameter will be used to add text which is to be displayed on your button. |
colors | Used Customized the button colors for different states (normal, disabled). |
elevation | Set different elevations for the default, pressed, and disabled states. |
contentPadding | Added padding inside the button around its content for a better appearance. |
interactionSource | Used a default MutableInteractionSource to handle interaction states. |
content | Added a Text composable inside the button, with customized text properties like font size, weight, style, and family. |
Step-by-Step Implementation
Step 1: Create a New Project.
To create a new project in the Android Studio Canary Version please refer to How to Create a new Project in Android Studio Canary Version with Jetpack Compose.
Step 2: Working with the MainActivity.kt file
Navigate to the app > java > your app’s package name and open the MainActivity.kt file. Inside that file add the below code to it.

Comments are added inside the code to understand the code in more detail.
MainActivity.kt:
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.Toast
import androidx.activity.*
import androidx.activity.compose.setContent
import androidx.compose.foundation.*
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.ui.graphics.*
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.*
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.*
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
MaterialTheme {
Surface(color = MaterialTheme.colorScheme.background) {
MyButton()
}
}
}
}
}
@Preview(showSystemUi = true)
@Composable
fun DefaultPreview() {
MaterialTheme {
MyButton()
}
}
@Composable
fun MyButton() {
Column(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
val context = LocalContext.current
Button(
onClick = {
Toast.makeText(context, "Welcome to Geeks for Geeks", Toast.LENGTH_LONG).show()
},
modifier = Modifier.padding(16.dp),
enabled = true,
shape = RoundedCornerShape(12.dp),
colors = ButtonDefaults.buttonColors(
contentColor = Color.Green,
containerColor = Color.Black
),
elevation = ButtonDefaults.buttonElevation(defaultElevation = 10.dp),
border = BorderStroke(width = 2.dp, brush = SolidColor(Color.Green)),
contentPadding = PaddingValues(
start = 20.dp,
top = 12.dp,
end = 20.dp,
bottom = 12.dp
),
interactionSource = remember { MutableInteractionSource() }
) {
Text(
text = "Geeks for Geeks",
fontSize = 16.sp,
fontWeight = FontWeight.Bold,
fontStyle = FontStyle.Italic,
fontFamily = FontFamily.Serif
)
}
}
}