In Android, Buttons are the most basic and frequently used UI element that lets users call a function or start a task from the application context. It is provided to the user to give input to the application with the help of a click. These Buttons are largely customizable and visual attributes can be altered according to the theme of the application. In this article, we will show you how you could create a Circular Button with an Icon in Android using Jetpack Compose.
.gif)
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in the Android Studio, please refer to How to Create a new Project in Android Studio with Jetpack Compose.
Note: Select Kotlin as the programming language.
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.
MainActivity.kt:
package com.geeksforgeeks.demo
import android.os.Bundle
import android.widget.Toast
import androidx.activity.*
import androidx.activity.compose.setContent
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.geeksforgeeks.demo.ui.theme.DemoTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
DemoTheme(dynamicColor = false, darkTheme = false) {
Surface(color = Color.White) {
MyContent()
}
}
}
}
}
@Preview(showSystemUi = true)
@Composable
fun MyContent(){
val mContext = LocalContext.current
Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally) {
// Create an Outlined Button and set the shape to CircleShape
OutlinedButton(onClick = { Toast.makeText(mContext, "This is a Circular Button with a + Icon", Toast.LENGTH_SHORT).show()},
modifier= Modifier.size(100.dp),
shape = CircleShape,
border= BorderStroke(5.dp, Color(0XFF0F9D58)),
contentPadding = PaddingValues(0.dp),
colors = ButtonDefaults.outlinedButtonColors(contentColor = Color.Blue)
) {
// Adding an Icon "Add" inside the Button
Icon(Icons.Default.Add ,contentDescription = "content description", tint=Color(0XFF0F9D58))
}
}
}