Compose Column with vertical scrollable modifier not working - android-jetpack-compose

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

Related

Jetpack Compose NestedScroll bounce to opposite direction

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 to get rid of white space from a card jetpack compose

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))
//....
)

How do I fix ConstraintLayout that include Balloon(gives strange results) in jetpack compose?

I'm trying to make a help pop ups in my first app. the problem that came up after making the pop up work is that the icon which I'm using becomes a button taking up whole screen height.
I'm using the only code I found for balloon popups in jetpack compose.
the layout is fine until I add the BalloonAnchor.
this is the code:
#Composable
fun GiveHelp(helpText: String) {
Surface{
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
ConstraintLayout {
val (icon, text) = createRefs()
Icon(
modifier = Modifier
.constrainAs(icon) {
top.linkTo(parent.top)
start.linkTo(parent.start)
},
painter = painterResource(id = R.drawable.ic_help),
contentDescription = "help Icon"
)
Text(
modifier = Modifier
.constrainAs(text) {
top.linkTo(icon.top)
start.linkTo(icon.end)
bottom.linkTo(icon.bottom)
}
.padding(horizontal = 10.dp),
text = "Is your task:"
)
BalloonAnchor(
reference = icon,
modifier = Modifier
.aspectRatio(0.1f),
balloon = BalloonUtils.getTitleBalloon(
context = context,
title = helpText,
lifecycle = lifecycleOwner
),
onAnchorClick = { balloon, anchor -> balloon.showAlignTop(anchor) }
)
}
}
}
The problem here is the aspectRatio you are using in the Modifier of BalloonAnchor. Try something like Modifier.aspectRatio(0.99f). Using this, your Icon will not take the entire screen height. Or, your can use something like below code to get a desirable look.
BalloonAnchor(
reference = icon,
modifier = Modifier
.height(40.dp),
balloon = BalloonUtils.getTitleBalloon(
context = context,
title = helpText,
lifecycle = lifecycleOwner
),
onAnchorClick = { balloon, anchor -> balloon.showAlignTop(anchor) }
)

Jetpack Compose - Fit Text in a specific box

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):

How to make card gradient in Jetpack compose?

#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*/){
}

Resources