I am working on this design where I have a column that has text and image on top and I have this card view but I want to remove this white space. The card is white I just added the blue to show the white space showing. How can I remove the white space. I want the R.color.purple_700 to fill that space.
Here is my code
Column(
modifier = modifier
.fillMaxSize()
) {
Column(
modifier = modifier
.fillMaxWidth()
.background(colorResource(id = R.color.purple_700))
.weight(2.0f),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
// Do I add the box here?
Image(
painter = painterResource(id = R.drawable.ic_launcher_background),
contentDescription = "Logo"
)
Text(
text = "Sign in to Continue",
color = (colorResource(id = R.color.white)),
fontSize = 28.sp,
modifier = Modifier.padding(8.dp)
)
}
Card(
shape = RoundedCornerShape(topStart = 32.dp, topEnd = 32.dp),
modifier = modifier
.fillMaxWidth()
.weight(5.0f), elevation = 8.dp
) {
Column(
modifier = modifier
.fillMaxSize()
.padding(horizontal = 16.dp)
.scrollable(scrollState, Orientation.Vertical),
horizontalAlignment = Alignment.CenterHorizontally
) {
// then some outlinedText
}
Just add the background modifier to the Card.
Card(
modifier = Modifier.background(colorResource(id = R.color.purple_700))
//....
)
Related
I am trying to create a similar-to-scrollview behavior with Jetpack Compose, which should be simple but I find it not working (I can't scroll the Column). I have been following the example from here:
https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/scrollers/VerticalScrollableActivity.kt#L104
And this is my code:
#Composable
fun MainScreen() {
val scrollState = rememberScrollState()
Column(
modifier = Modifier
.fillMaxSize()
.scrollable(
state = scrollState,
orientation = Orientation.Vertical
)
) {
Text(
text = "This is my first Compose App",
fontSize = 24.sp,
fontWeight = FontWeight.Light,
lineHeight = 40.sp,
color = Color.Black,
modifier = Modifier.padding(top = 32.dp, start = 24.dp)
)
Row(
modifier = Modifier
.fillMaxWidth()
.background(Color(0xFF6A0DAD))
.height(2200.dp))
{}
}
}
as mentioned #bylazy using Modifier.verticalScroll(scrollState) solves it
When I use a LazyColumn nested in Column that can scroll, sometimes I fast scroll the inner LazyColumn, the columns will bounce to the opposite direction.
Like the attached picture, when LazyColumn scroll to top, the outer Column and the inner LazyColumn will bounce to bottom.
How can I remove this effect?
Thank you in advance!
Note: the compose version is 1.2.1
code is like this:
#Composable
fun NestedScrollSample() {
val dataList1 = mutableListOf<Int>()
for (i in 0..6) {
dataList1.add(i)
}
Column(
content = {
Box(
modifier = Modifier
.background(Color.Blue)
.height(360.dp)
.fillMaxWidth()
)
Text(text = "List", textAlign = TextAlign.Center, modifier = Modifier.fillMaxWidth())
LazyColumn(
content = {
itemsIndexed(dataList1) { index, item ->
Text(
text = "$index $item",
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 8.dp)
)
Divider(thickness = 1.dp, color = Color(0XFFE5E5E5))
}
}, modifier = Modifier
.background(Color(0xFFF5F7FA))
.height(180.dp)
.padding(horizontal = 4.dp)
)
Box(
modifier = Modifier
.background(Color.Green)
.height(360.dp)
.fillMaxWidth()
)
}, modifier = Modifier
.fillMaxSize()
.verticalScroll(state = rememberScrollState())
)
}
How can I fit a text, inside a specific Box / Container (i.e of Size 100.dp)
Is there a way to Fit a string inside a Box, in Compose?
Here is what I tried:
#Composable
fun playText() {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color(0xFF242E38)),
contentAlignment = Alignment.Center
) {
Row(modifier = Modifier
.background(Color(0xFFEEEEEE))
.width(100.dp)
.height(100.dp)
) {
Text(
text = "FIT ME",
modifier = Modifier
.fillMaxSize()
.wrapContentSize(
Alignment.Center,
unbounded = true
)
.requiredSize(100.dp),
color = Color(0xFF242E38),
fontSize = 50.sp,
style = TextStyle.Default.copy(
background = Color(0xFFEAC43D)
)
)
}
}
}
#Preview(showBackground = true)
#Composable
fun show() {
playText()
}
Here is the current result:
Here is the expected result (which I achieved by decreasing the font size, just to demonstrate what is expected):
#Composable fun Gradientcard() {
val horizontalGradientBrush = Brush.horizontalGradient(
colors = listOf(
Blue,
lightBlue
)
)
Card(modifier = modifier = Modifier
.background(brush = horizontalGradientBrush),shape = RoundedCornerShape(20.dp)){
Text(
text = "sub 1",
)
}
This method made background of card as gradient but not the card. Card color is still white.
Output
Instead of using modifier in Card try creating Box layout inside Card and add gradient code inside it.
Card(
modifier = Modifier
.fillMaxWidth()
.height(175.dp),
elevation = 4.dp,
shape = RoundedCornerShape(24.dp),
) {
Box(
Modifier
.background(
/* Your code*/ ) {
Text(
text = "Card Gradient Background",
)
}
}
Just cut that modifier from theText, and paste it in the Card
Card(modifier = ... /*Paste*/){
}
I've got a some dynamic content that can vary in size and below that is a circular image. The problem is that when the content becomes too big, the image overflows the content.
The ideal thing would be that if dynamic sized content is too big, the image should be hidden instead. Is it possible to hide the image (the box) if it will not fit?
Example code:
#Composable
fun ImageTest(modifier: Modifier = Modifier,
outlineColor: Color = Color.White,
outlineSize: Dp = 16.dp,
image: Int) {
Column(
horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier
.fillMaxSize()
) {
Text("Content that can be of different sizes", style = MaterialTheme.typography.h2, modifier = Modifier.height(550.dp).background(Color.Red))
// Circular image - HIDE THE BOX IF IT DOESN'T FIT
Box(
modifier = modifier
.background(color = outlineColor, shape = CircleShape)
.padding(outlineSize)
.aspectRatio(ratio = 1f)
) {
Image(
painter = painterResource(id = image),
contentDescription = null,
contentScale = ContentScale.Fit,
modifier = Modifier.fillMaxSize()
)
}
}
}
Replace image container Box with BoxWithConstraints. Then, inside BoxWithConstraintsScope you can check how much space is available for your view, and if you don't have enough - don't display it.
BoxWithConstraints(
modifier = modifier
.background(color = outlineColor, shape = CircleShape)
.padding(outlineSize)
.aspectRatio(ratio = 1f)
) {
if (maxHeight > 200.dp) {
Image(
painter = painterResource(id = image),
contentDescription = null,
contentScale = ContentScale.Fit,
modifier = Modifier.fillMaxSize()
)
}
}
If you wanna hide box background too, move it inside if.
If you want add an animation, replace if (maxHeight > 200.dp) { with AnimatedVisibility(visible = maxHeight > 200.dp) {