Skip to content
Related Articles

Related Articles

Button in Android using Jetpack Compose

View Discussion
Improve Article
Save Article
  • Last Updated : 08 Sep, 2021
View Discussion
Improve Article
Save Article

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



onClickTo perform an action when your button is clicked by the user.
modifierthis parameter is used to add padding to our button. 
enabledto enable or disable your button. 
borderthis parameter is used to add 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. 

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.


import android.os.Bundle
import android.widget.Toast
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Phone
import androidx.compose.runtime.*
import androidx.compose.runtime.savedinstancestate.savedInstanceState
import androidx.compose.ui.Alignment
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.gfgapp.ui.GFGAppTheme
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.*
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        setContent {
            GFGAppTheme {
                // A surface container using the 'background'
                  // color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    // at below line we are calling
                      // our function for button.
// @Preview function is use to see preview
// for our composable function in preview section.
@Preview(showBackground = true)
fun DefaultPreview() {
    GFGAppTheme {
          // we are passing our composable
        // function to display its preview.
fun MyButton() {
            // we are using column to align our
            // imageview to center of the screen.
            modifier = Modifier.fillMaxWidth().fillMaxHeight(),
            // below line is used for specifying
            // vertical arrangement.
            verticalArrangement = Arrangement.Center,
            // below line is used for specifying
            // horizontal arrangement.
            horizontalAlignment = Alignment.CenterHorizontally,
            ) {
        // below line is use to get
        // the context for our app.
        val context = ContextAmbient.current
        // below line is use to create a button.
                // below line is use to add onclick
                // parameter for our button onclick
                onClick = {
                    // when user is clicking the button
                    // we are displaying a toast message.
                    Toast.makeText(context, "Welcome to Geeks for Geeks", Toast.LENGTH_LONG).show()
                // in below line we are using modifier
                // which is use to add padding to our button
                modifier = Modifier.padding(all = Dp(10F)),
                // below line is use to set or
                // button as enable or disable.
                enabled = true,
                // below line is use to
                // add border to our button.
                border = BorderStroke(width = 1.dp, brush = SolidColor(Color.Blue)),
                // below line is use to add shape for our button.
                shape = MaterialTheme.shapes.medium,
        // below line is use to
        // add text on our button
            Text(text = "Geeks for Geeks", color = Color.White)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!