I have a LazyColumn that has a Button that I would like to Toggle when pressed, how do you implement this in Android?
This is what I have at the moment:
LazyColumn (
verticalArrangement = Arrangement.spacedBy(10.dp),
modifier = Modifier
.padding(horizontal = 10.dp, vertical = 10.dp)
)
{
items(filtered) { sound ->
Row {
// Share Button
IconToggleButton(
checked = false,
onCheckedChange = {
if (SoundService.isPlaying()) {
Toast.makeText(baseContext, "Stop playing", Toast.LENGTH_SHORT).show()
val intent = Intent(baseContext, SoundService::class.java).also {
it.action = SoundService.Actions.STOP.toString()
baseContext?.startForegroundService(it)
}
}
else {
Toast.makeText(baseContext, "play: " + sound, Toast.LENGTH_SHORT).show()
val projectDirAbsolutePath = baseContext?.filesDir?.toPath()
val file = projectDirAbsolutePath?.toString() + "/" + sound
val intent = Intent(baseContext, SoundService::class.java).also {
it.action = SoundService.Actions.START.toString()
it.putExtra("sound", file);
baseContext?.startForegroundService(it)
}
}
},
modifier = Modifier
.align(Alignment.CenterVertically)
)
{
Icon(Icons.Filled.PlayArrow, contentDescription = null)
}
Text(text = sound, fontWeight = FontWeight.Bold, fontSize = 24.sp, modifier = Modifier.align(Alignment.CenterVertically))
}
}
}
I have tried IconButton and am now looking at IconToggleButton, so far I have not been able to find an answer...
Here is a solution for the
Togglebutton state inLazyColumn. You need to have a data class which will save the current state ofIconToggleButtonwhich we will use for the showing Data based on that.When a user clicks on the toggle button we will match it with the current index and make it selected and other items deselected.
Preview
Complete Code