
Creating a third-party signin button can be a daunting task, but with Jetpack Compose, it's easier than ever to create beautiful and functional UI components for Android app.
In this tutorial, we'll be using Jetpack Compose to create a Sign in button that can be easily customized to fit your app's branding and style.
To start, let's take a look at the final product we'll be creating. The signin button will be a rounded rectangle with a custom icon and text label, as shown in the interactive demo application below :
Wonderful! Now, let's get into the code. First, we'll need to create a Composable function that will define the layout and appearance of the button. We'll start by creating a MaterialComponent and customizing it with the appropriate colors and shapes:
@Composable
fun SignInButton(
text: String,
loadingText: String = "Signing in...",
icon: Painter,
isLoading: Boolean = false,
borderColor: Color = Color.LightGray,
backgroundColor: Color = Color.White,
progressIndicatorColor: Color = MaterialTheme.colors.primary,
textStyle: TextStyle = MaterialTheme.typography.button,
onClick: () -> Unit
) {
Surface(
modifier = Modifier.clickable(
enabled = !isLoading,
onClick = onClick
).clip(RoundedCornerShape(4.dp)),
shape = RoundedCornerShape(4.dp),
border = if (borderColor != Color.Transparent)
BorderStroke(width = 1.dp, color = borderColor)
else
null,
color = backgroundColor
) {
Row(
modifier = Modifier.wrapContentSize()
.padding(16.dp)
.animateContentSize(
animationSpec = tween(
durationMillis = 300,
easing = LinearOutSlowInEasing
)
),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center,
) {
Icon(
painter = icon,
contentDescription = "SignInButton",
tint = Color.Unspecified,
modifier = Modifier.size(20.dp)
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = if (isLoading) loadingText else text,
style = textStyle
)
if (isLoading) {
Spacer(modifier = Modifier.width(16.dp))
CircularProgressIndicator(
modifier = Modifier
.height(16.dp)
.width(16.dp),
strokeWidth = 3.dp,
color = progressIndicatorColor
)
}
}
}
}
That's It! The SignInButton function code provided above contains the implementation of the Sign in button we've been working on. The function accepts several parameters, such as button text, icon, and styling options, to create a rounded rectangle button with a custom icon and label. The button has a loading state, indicated by a CircularProgressIndicator component, and it's implemented using the Surface composable. The Row and Spacer composables are used to create the layout of the button contents.
This code implements the demo application using this SignInButton function :
@Composable
fun LoginDemo() {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(
16.dp,
Alignment.CenterVertically
),
) {
var isLoading by remember { mutableStateOf(0) }
val buttonStyle = MaterialTheme.typography.button.copy(
fontWeight = FontWeight.Bold
)
Text(
text = "Click it!",
style = MaterialTheme.typography.h4.copy(
fontWeight = FontWeight.Bold,
color = Color.White
)
)
Spacer(modifier = Modifier.height(16.dp))
SignInButton(
text = "Sign in with Google",
isLoading = isLoading == 1,
icon = rememberVectorPainter(Icons.Google),
textStyle = buttonStyle.copy(
color = Color.Black
),
onClick = {
isLoading = 1
})
SignInButton(
text = "Sign in with Twitter",
isLoading = isLoading == 2,
icon = rememberVectorPainter(Icons.Twitter),
textStyle = buttonStyle.copy(
color = Color.White
),
onClick = {
isLoading = 2
},
progressIndicatorColor = Color.White,
backgroundColor = Color(0xFF2696ea),
borderColor = Color.Transparent
)
}
}
With just a few lines of code, we've created a beautiful and functional signin button that can be easily integrated into any Android app. Feel free to experiment with different colors, fonts, and shapes to create a button that matches your app's branding and style.
Conclusion
Congratulations, you've successfully created a third-party Sign in button using Jetpack Compose!
Remember, Jetpack Compose is a powerful tool that can help you streamline your app development process and create beautiful, functional UIs in less time than ever before. If you enjoyed this tutorial, be sure to check out my other articles on Jetpack Compose and Android development.
Thanks for reading, and happy coding!


