Open In App

Change Button Size in Android Jetpack Compose

Last Updated : 13 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Android, Button is a very common UI element that is used to call a function or perform a task when clicked by the user. A Button by default occupies specific space depending upon the text that it displays. If the Button text is longer or has a larger font size, the Button width, and height increase. Similarly, if the Button text is smaller or has a smaller font size, the Button width and height decrease. However, we can set pre-defined width and height to a Button irrespective of the text attributes.

Button in Android

So in this article, we will show you how you could change the Button size in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.changebuttonsize
  
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.geeksforgeeks.changebuttonsize.ui.theme.ChangeButtonSizeTheme
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // Calling the composable function
            // to display the elements and the content
            MainContent()
        }
    }
}
  
// Creating a composable 
// function to display the Top bar
@Composable
fun MainContent(){
    Scaffold(
        topBar = {TopAppBar(
            title = {Text(
                "GFG | Change Button Size",
                color = Color.White)},
            backgroundColor = Color(0xff0f9d58)
        ) },
        content = { MyContent()}
    )
}
  
// Creating a composable function to 
// display three buttons of different sizes.
// This function is called in the 
// content of the above function
@Composable
fun MyContent(){
    
      // Creating a normal button at a margin of 100dp from the top
    Column(Modifier.fillMaxWidth().absolutePadding(10.dp, 100.dp, 10.dp, 0.dp), horizontalAlignment = Alignment.CenterHorizontally) {
        Button(onClick = { /*TODO*/ },
            colors = ButtonDefaults.buttonColors(backgroundColor = Color(0XFF0F9D58)),
            ) {
            Text("Regular Button", color = Color.White)
        }
    }
  
    // Creating a Bigger button with height and width as 100dp and 200dp, 
    // font size as 30sp, at a margin of 200dp from the top
    Column(Modifier.fillMaxWidth().absolutePadding(10.dp, 200.dp, 10.dp, 0.dp), horizontalAlignment = Alignment.CenterHorizontally) {
        Button(onClick = { /*TODO*/ },
            colors = ButtonDefaults.buttonColors(backgroundColor = Color(0XFF0F9D58)),
            modifier = Modifier.height(100.dp).width(200.dp)
        ) {
            Text("Big Button", color = Color.White, fontSize = 30.sp)
        }
    }
  
    // Creating a Smaller button with height and width as 30dp and 100dp, 
    // font size as 7sp, at a margin of 350dp from the top
    Column(Modifier.fillMaxWidth().absolutePadding(10.dp, 350.dp, 10.dp, 0.dp), horizontalAlignment = Alignment.CenterHorizontally) {
        Button(onClick = { /*TODO*/ },
            colors = ButtonDefaults.buttonColors(backgroundColor = Color(0XFF0F9D58)),
            modifier = Modifier.height(30.dp).width(100.dp)
        ) {
            Text("Small Button", color = Color.White, fontSize = 7.sp)
        }
    }
}
  
// Displaying the preview in the 
// Android Studio IDE Real time emulator
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MainContent()
}


Output:

When you run the application, you will see three buttons of different fixed sizes as shown in the below image.

Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads