Skip to content

Instantly share code, notes, and snippets.

@developerchunk
Last active March 12, 2025 12:26
Show Gist options
  • Save developerchunk/83e411405bd094ffd1ab511799318e8f to your computer and use it in GitHub Desktop.
Save developerchunk/83e411405bd094ffd1ab511799318e8f to your computer and use it in GitHub Desktop.
Create Custom Switch Button Jetpack in Compose | Full Documentation on Medium- https://link.medium.com/eWGtmZ8Auub
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.customthemechange.ui.theme.Purple500
@Composable
fun CustomSwitchButton(
switchPadding: Dp,
buttonWidth: Dp,
buttonHeight: Dp,
value: Boolean
) {
val switchSize by remember {
mutableStateOf(buttonHeight-switchPadding*2)
}
val interactionSource = remember {
MutableInteractionSource()
}
var switchClicked by remember {
mutableStateOf(value)
}
var padding by remember {
mutableStateOf(0.dp)
}
padding = if (switchClicked) buttonWidth-switchSize-switchPadding*2 else 0.dp
val animateSize by animateDpAsState(
targetValue = if (switchClicked) padding else 0.dp,
tween(
durationMillis = 700,
delayMillis = 0,
easing = LinearOutSlowInEasing
)
)
Box(
modifier = Modifier
.width(buttonWidth)
.height(buttonHeight)
.clip(CircleShape)
.background(if (switchClicked) Purple500 else Color.LightGray)
.clickable(
interactionSource = interactionSource,
indication = null
) {
switchClicked = !switchClicked
}
){
Row(modifier = Modifier.fillMaxSize().padding(switchPadding)) {
Box(
modifier = Modifier
.fillMaxHeight()
.width(animateSize)
.background(Color.Transparent)
)
Box(modifier = Modifier
.size(switchSize)
.clip(CircleShape)
.background(Color.White))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment