Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
Button in Android using Jetpack Compose
Next article icon

Button in Android using Jetpack Compose

Last Updated : 20 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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 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.

text-compose-dir


Comments are added inside the code to understand the code in more detail.

MainActivity.kt:

MainAcitivity.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             )         }     } } 

Output:


Next Article
Button in Android using Jetpack Compose

C

chaitanyamunje
Improve
Article Tags :
  • Technical Scripter
  • Kotlin
  • Android
  • Technical Scripter 2020
  • Android-Jetpack

Similar Reads

    Switch Button in Android using Jetpack Compose
    A Switch or a Switch Button in Android is a UI element that is used to switch between two states upon click. It can be assumed as a Boolean button with two different values. Some states where you may find a Switch in your Android device can be WIFI ON and OFF, Bluetooth ON and OFF, Dark Mode and Lig
    2 min read
    Checkbox in Android using Jetpack Compose
    The checkbox is a composable function that is used to represent two states of any item in Android. It is used to differentiate an item from the list of items. In this article, we will take a look at the implementation of Simple Checkbox in Android using Jetpack Compose. Attributes of CheckboxAttribu
    2 min read
    Icon Toggle Button in Android using Jetpack Compose
    Toggle Buttons are used in most applications. These buttons are used to perform multiple operations. Toggle buttons are seen used in many social media applications such as Instagram. In social media apps, we can get to see a heart icon that is used to like the image. We can also unlike that image by
    3 min read
    Change Button Size in Android Jetpack Compose
    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 increa
    3 min read
    Motion Layout Button in Android Jetpack Compose
    Motion Layout is a special version of Constraint layout. With the help of motion layout, we can add animations to the widgets within the layout and change the position of that widget dynamically. In this article, we will take a look at How to implement Motion Layout animation on buttons in Android u
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences