Nino
10/22/2024, 11:12 AMText
would ? Also, it seems there's a bug when their parent's height is 0.dp (see that blue background still appearing with the parent height being at 0.dp)
I'm so confused why would wrapping a Text
in a Button
would change so dramatically its behavior when dealing with its height. Am I missing a key concept ? All of this looks like a bug to me (code example in thread)
Video example: 1: using Text
, 2: using Button
Nino
10/22/2024, 11:13 AMpackage fr.delcey.endnewpostcompose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Slider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
class LayoutActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Box(Modifier.fillMaxSize()) {
var sliderPosition by remember { mutableFloatStateOf(0f) }
Column(
modifier = Modifier
.align(Alignment.TopCenter)
.fillMaxWidth()
.height((sliderPosition * 200).dp),
) {
CroppableContent()
}
Column(
modifier = Modifier
.align(Alignment.BottomCenter)
) {
Slider(
value = sliderPosition,
onValueChange = { sliderPosition = it }
)
Text(text = sliderPosition.toString())
}
}
}
}
@Composable
private fun CroppableContent() {
Text(
text = "Lorem ipsum dolor sit amet",
modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.background(Color.Red)
)
Text(
text = "Lorem ipsum dolor sit amet",
modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.background(Color.Blue)
)
}
}
Nino
10/22/2024, 11:13 AM@Composable
private fun CroppableContent() {
Button(
onClick = { /*TODO*/ },
modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.background(Color.Red)
) {
Text(text = "Lorem ipsum dolor sit amet")
}
Button(
onClick = { /*TODO*/ },
modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.background(Color.Blue)
) {
Text(text = "Lorem ipsum dolor sit amet")
}
}
Stylianos Gakis
10/22/2024, 11:22 AMSurface(Modifier.clickable {}) {
Text("text", Modifier.padding...)
}
You would experience a more "normal" behavior without that many surprises.Nino
10/22/2024, 11:25 AMStylianos Gakis
10/22/2024, 11:28 AMNino
10/22/2024, 11:32 AMLottieAnimation
or a Coil Image.
What do you mean by "material design system and specs" ? I just want to make some text / button appear dynamically (by animating their height), I don't feel like I'm breaking the material specs ? I'm so confused about everything here I feel like I'm missing something. I'm not allowed to modify the height of Material Components or their parents ?Stylianos Gakis
10/22/2024, 11:40 AMminSize
there for example.
Many of the material components do allow you some flexibility in the way they are implemented, but most do not allow you do just anything you might want. The flexibility is limited.
You might be interested in this article https://proandroiddev.com/opinion-jetpack-compose-needs-a-design-system-layer-dc579fde79b2. It's not just who you is surprised by these facts. Many people have opinions about this space as a whole.Nino
10/22/2024, 11:43 AMStylianos Gakis
10/22/2024, 11:54 AMNino
10/22/2024, 12:02 PMStylianos Gakis
10/22/2024, 12:03 PMNino
10/22/2024, 12:04 PMAlbert Chang
10/22/2024, 12:06 PMCompositionLocalProvider(
LocalMinimumInteractiveComponentEnforcement provides false
) {
...
}
But using something like AnimatedVisibility
is definitely the better choice.Nino
10/22/2024, 12:09 PMAlbert Chang
10/22/2024, 12:17 PMAnimatedVisibility
if you want to animate the appearance.
If you really want to control the height yourself, you can apply this to the button.
Modifier
.graphicsLayer(clip = true)
.wrapContentHeight(align = <http://Alignment.Top|Alignment.Top>, unbounded = true)
Stylianos Gakis
10/22/2024, 12:19 PMAnimatedVisibility(
visible = ...,
enter = expandVertically(expandFrom = <http://Alignment.Top|Alignment.Top>),
exit = shrinkVertically(shrinkTowards = <http://Alignment.Top|Alignment.Top>),
)
you got a lot of control there regarding the animation specs.Nino
10/22/2024, 12:29 PMModifier
.graphicsLayer(clip = true)
.wrapContentHeight(align = <http://Alignment.Top|Alignment.Top>, unbounded = true)
is the way to keep the same behavior as XML. Helps me tremendously. Thank you !
The AnimatedVisibility (or animate*AsState
) doesn't seem to fit my need, as I need sometime to bind the animation to some user gesture, so I need to tight control around the "percentage" of the animation done (and also revert animation, etc)