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 clicking on the same icon. In this article, we will look at How to Create an Icon Toggle Button in Android using Jetpack Compose.
A sample gif is given below to get an idea about what we are going to do in this article.

Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version.
We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
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.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.animateColor
import androidx.compose.animation.core.*
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.geeksforgeeks.demo.ui.theme.DemoTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
// force setting it to light theme and turning off dynamic theme
DemoTheme(dynamicColor = false, darkTheme = false) {
Surface(
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
) {
DisplayToggleButton()
}
}
}
}
}
@SuppressLint("UnusedTransitionTargetStateParameter")
@Composable
fun DisplayToggleButton() {
// set the state of our checkbox.
val checkedState = remember { mutableStateOf(false) }
Column(
Modifier
.fillMaxSize()
.fillMaxHeight()
.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = if (checkedState.value) "Like" else "Dislike",
modifier = Modifier
.fillMaxWidth()
.padding(20.dp),
textAlign = TextAlign.Center,
style = MaterialTheme.typography.headlineMedium
)
Spacer(modifier = Modifier.height(8.dp))
// create a toggle button with a custom icon
IconToggleButton(
// set default check state
checked = checkedState.value,
// set on check change
onCheckedChange = {
checkedState.value = !checkedState.value
},
modifier = Modifier.padding(10.dp)
) {
// initialize transition
val transition = updateTransition(checkedState.value)
// set color of icon
val tint by transition.animateColor(label = "iconColor") { isChecked ->
if (isChecked) Color.Red else Color.Black
}
// define transition
val size by transition.animateDp(
transitionSpec = {
// on below line we are specifying transition
if (false isTransitioningTo true) {
// on below line we are specifying key frames
keyframes {
// on below line we are specifying animation duration
durationMillis = 250
// on below line we are specifying animations.
30.dp at 0 using LinearOutSlowInEasing // for 0-15 ms
35.dp at 15 using FastOutLinearInEasing // for 15-75 ms
40.dp at 75 // ms
35.dp at 150 // ms
}
} else {
spring(stiffness = Spring.StiffnessVeryLow)
}
},
label = "Size"
) { 30.dp }
// create icon for toggle button.
Icon(
imageVector = if (checkedState.value) Icons.Filled.Favorite else Icons.Filled.FavoriteBorder,
contentDescription = "Icon",
tint = tint,
modifier = Modifier.size(size)
)
}
}
}