https://kotlinlang.org logo
#compose
Title
# compose
d

Denis

02/08/2021, 4:37 PM
The code in the thread causes the next exception on change of a theme:
Copy code
java.lang.IllegalArgumentException: Layer is redrawn for LayoutNode in state NeedsRelayout [LayoutNode@5aef729 children: 0 measureBlocks: MeasuringIntrinsicsMeasureBlocks@d83eaae{ measureBlock=androidx.compose.foundation.layout.BoxKt$boxMeasureBlocks$1@340204f }]
There was a similar issue https://github.com/android/compose-samples/issues/129. I don't know if it's the same problem.
Copy code
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            var dark by remember { mutableStateOf(false) }
            ExperimentsTheme(darkTheme = dark) {
                Surface(color = MaterialTheme.colors.background) {
                    ListItemsApp(dark, setDark = { dark = it })
                }
            }
        }
    }
}

@Composable
fun ListItemsApp(dark: Boolean, setDark: (Boolean) -> Unit) {
    LazyColumn {
        val paddingModifier = Modifier.padding(bottom = 8.dp)
        item {
            ListItem(
                modifier = Modifier.clickable(onClick = { setDark(false) }),
                trailing = { RadioButton(selected = !dark, onClick = { setDark(false) }) },
                text = { Text("Light") },
            )
            ListItem(
                modifier = Modifier.clickable(onClick = { setDark(true) }),
                trailing = { RadioButton(selected = dark, onClick = { setDark(true) }) },
                text = { Text("Dark") },
            )
            Divider()
        }

        for (i in (1..7)) {
            item {
                Text(
                    "Block $i",
                    modifier = Modifier.padding(16.dp),
                    style = MaterialTheme.typography.subtitle1
                )
                ListItem(
                    text = { Text("$i. List item with switch") },
                    secondaryText = { Text("Short secondary text") },
                    trailing = { Switch(checked = i % 4 == 0, onCheckedChange = {}) },
                )
                ListItem(
                    text = { Text("$i. List item with radio button") },
                    secondaryText = {
                        Text(
                            "Somewhat long secondary text that may wrap on the second line, but I'm not sure about that",
                            modifier = paddingModifier
                        )
                    },
                )
            }
        }
    }
}
It's the shortest example I could come up with. Anything else I delete and the bug is gone.
The bug appeared in my app when I added
paddingModifier
. I'd rather not add it, but without it
ListItem
with a long secondary text doesn't have enough bottom padding. And if I add padding to a whole ListItem, the ripple does not get drawn on that padding. I know about
singleLineSecondaryText
, but how do I know if the text is too long?
Copy code
compose_version = '1.0.0-alpha11'
classpath 'com.android.tools.build:gradle:7.0.0-alpha05'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21-2"
It's reproducible on 2 physical devices. It does NOT reproduce on emulator.
a

Andrey Kulikov

02/09/2021, 12:14 AM
could you please file a bug? thanks
what version of compose do you use?
d

Denis

02/10/2021, 5:43 PM
@Andrey Kulikov, 1.0.0-alpha11. I haven't filed a but yet. Now I see this exception even without
paddingModifier
. But with
singleLineSecondaryText = false
for some of `ListItem`s.
Looks like if you wrap all things in each
item
in a
Column
, the exception is gone