Advertisement

Responsive Advertisement

Custom Button With Icon & ProgressBar in jetpack compose

  •  Step 1 :- 
@Composable
fun GoogleButton(
modifier: Modifier = Modifier,
text: String = "Sign Up with Google",
loadingText: String = "Creating Account...",
icon: Int = R.drawable.ic_google_logo,
shape: Shape = MaterialTheme.shapes.medium,
borderColor: Color = Color.LightGray,
backgroundColor: Color = MaterialTheme.colorScheme.surface,
progressIndicatorColor: Color = MaterialTheme.colorScheme.primary,
onClicked: () -> Unit,
) {
var clicked by remember { mutableStateOf(false) }
Surface(
modifier = modifier
.clip(MaterialTheme.shapes.medium)
.clickable {
clicked = !clicked
onClicked()
},
shape = shape,
border = BorderStroke(width = 1.dp, color = borderColor),
color = backgroundColor
) {
Row(
modifier = Modifier
.padding(
start = 12.dp,
end = 16.dp,
top = 12.dp,
bottom = 12.dp
)
.animateContentSize(
animationSpec = tween(
durationMillis = 300,
easing = LinearOutSlowInEasing
)
),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Icon(
painter = painterResource(id = icon),
contentDescription = "Google Button",
tint = Color.Unspecified
)
Spacer(modifier = Modifier.width(8.dp))
Text(text = if (clicked) loadingText else text)
if (clicked) {
Spacer(modifier = Modifier.width(16.dp))
CircularProgressIndicator(
modifier = Modifier
.height(16.dp)
.width(16.dp),
strokeWidth = 2.dp,
color = progressIndicatorColor
)
}
}
}
}
  • Step 2 :-   In Component activity,
    GoogleButton(
    Modifier.fillMaxWidth(0.7f).align(Alignment.CenterHorizontally).padding(top = 24.dp),
    text = "Sign Up with Click",
    "Wait a moment..",
    R.drawable.ic_google_logo,
    shape = ShapeDefaults.Medium,
    borderColor = Color.White,
    backgroundColor = MaterialTheme.colorScheme.primary,
    progressIndicatorColor = Color.White
    ) {
    Log.e("rohan", "onCreate: button clicked", )
    }
  • ScreenShot :-













Post a Comment

0 Comments