Reputation: 1001
I have a problem with RadioButton component in my Jetpack Compose application. I have some RadioButtons with text and this have a lot of padding by default. Can I remove this padding or to set a custom padding to avoid a lot of space between each?
Currently I have this:
My code is:
Column {
MyEnum.values().filter { rb -> rb.visible }.forEach { rb ->
Row(
Modifier
.fillMaxWidth()
.padding(horizontal = 0.dp, vertical = 0.dp)
.clickable(
interactionSource = interactionSource,
indication = null
) {
TODO()
},
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(
selected = (rb.position == selectedOption),
onClick = {
TODO()
},
colors = RadioButtonDefaults.colors(
selectedColor = DialogOutlinedTextFocus,
unselectedColor = DialogOutlinedTextUnfocus
)
)
Text(
text = stringResource(id = rb.idText),
color = Color.Black,
fontSize = 14.sp,
modifier = Modifier
.padding(horizontal = 3.dp, vertical = 2.dp)
)
}
}
}
I tried with contentPadding, but this property does not exist in RadioButton component.
Upvotes: 11
Views: 4722
Reputation: 2114
CompositionLocalProvider(
LocalMinimumInteractiveComponentSize provides 0.dp
) {
RadioButton(
selected = selected,
onClick = onSelect,
modifier = Modifier
.padding(horizontal = Spacing.nil, vertical = Spacing.nil)
.semantics { contentDescription = option.name },
)
}
Upvotes: 0
Reputation: 81
@Thracian answer's about checkbox works also with RadioButton
CompositionLocalProvider(LocalMinimumInteractiveComponentEnforcement provides false) {
RadioButton(
selected = selected,
onClick = {...},
)
}
Upvotes: 6
Reputation: 950
The source code for RadioButton.kt
sets the padding modifier at line 108. With modifiers, order matters. Even if you provide your own padding modifier it will be overridden by line 108.
The sizing values are hardcoded at the bottom of the file.
If you really want to "reduce the padding", apply a size
modifier. I use 20.dp
because it's equal to radioButtonSize
in RadioButton.kt
so the button doesn't get clipped. This should work for your purposes since the entire row is clickable.
RadioButton(
modifier = Modifier.size(20.dp),
selected = selected,
onClick = { TODO() },
)
Although, you're probably better off in the long term making custom components you can control. Luckily, the source code is ready available. Just copy, paste and adjust to your needs.
Upvotes: 31
Reputation: 3359
You could specify the Row height in the Row.Modifier like this:
Row(
Modifier
.fillMaxWidth()
//HERE YOU GO
.height(30.dp)
.padding(horizontal = 0.dp, vertical = 0.dp)
Upvotes: 1