How can I put the text in the center of the total clickable area and still in the start of the component?
Like an android:layout_gravity="center" with margin.
You can use contentAlignment = Alignment.CenterStart with Box.
Box(
modifier = Modifier
.clickable {
}
.fillMaxWidth()
.height(46.dp),
contentAlignment = Alignment.CenterStart
) {
Text("Hello World")
}
Related
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())
)
}
I run either Code A1 or Code A2, and get the same result Image A1A2.
I run Code B, and get the result Image B.
I run either Code C1 or Code C2, and get the same result Image C1C2.
1: Either Canvas( modifier = Modifier... in ScreenHome_Table of Code A1 or Canvas( modifier = parentModifier... in ScreenHome_Table of Code A2 get the the same result, why? and why doesn't Canvas(modifier = Modifier.fillMaxSize().. in ScreenHome_Table of Code A1 fill in full screen?
2: Row( modifier = parentModifier.fillMaxSize( in Code B expand child element to full screen of parent element, why ?
3: The Canvas in both Code C1 and Code C2 are disappeared, why?
Code A1
#Composable
fun ScreenHome(
rootModifier: Modifier = Modifier,
...
) {
Scaffold(
modifier = rootModifier.fillMaxSize(),
...
) { paddingValues ->
Column(
modifier = Modifier.fillMaxSize()
) {
ScreenHome_Watch(
parentModifier = Modifier.fillMaxWidth().weight(0.6f)
)
ScreenHome_Info(
parentModifier = Modifier.fillMaxWidth()
)
ScreenHome_Table(
parentModifier = Modifier.fillMaxWidth().weight(0.4f)
)
}
}
}
#Composable
fun ScreenHome_Watch(
parentModifier: Modifier = Modifier,
){
Box(
modifier = parentModifier
.background(color = Color.Yellow)
) {
Canvas(
modifier = Modifier.fillMaxSize()
) {
}
}
}
#Composable
fun ScreenHome_Info(
parentModifier: Modifier = Modifier
){
Row(
modifier = parentModifier
.background(color = Color.Gray),
verticalAlignment= Alignment.CenterVertically
){
Text("Hello")
}
}
#Composable
fun ScreenHome_Table(
parentModifier: Modifier = Modifier,
) {
Box(
modifier = parentModifier
) {
Canvas(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Green)
) {
}
}
}
Code A2
//The other code is same as Code A1
#Composable
fun ScreenHome_Table(
parentModifier: Modifier = Modifier,
) {
Box(
modifier = parentModifier
) {
Canvas(
modifier = parentModifier
.fillMaxSize()
.background(color = Color.Green)
) {
}
}
}
Image A1A2
Code B
//The other code is same as Code A1
#Composable
fun ScreenHome_Info(
parentModifier: Modifier = Modifier
){
Row(
modifier = parentModifier
.fillMaxSize()
.background(color = Color.Gray),
verticalAlignment= Alignment.CenterVertically
){
Text("Hello")
}
}
Image B
Code C1
//The other code is same as Code A1
#Composable
fun ScreenHome_Table(
parentModifier: Modifier = Modifier,
) {
Box(
modifier = parentModifier
) {
Canvas(
modifier = Modifier
.background(color = Color.Green) //The Canvas is disappear;
) {
}
}
}
Code C2
//The other code is same as Code A1
#Composable
fun ScreenHome_Table(
parentModifier: Modifier = Modifier,
) {
Box(
modifier = parentModifier
) {
Canvas(
modifier = parentModifier
.background(color = Color.Green) //The Canvas is disappear;
) {
}
}
}
Image C1C2
Using an instance of Modifier that you got as a parameter twice is something you definitely shouldn't do. In your case though, it behaves the same as if you'd use Modifier.fillMaxWidth().weight(0.4f) instead of parentModifier on the Canvas, since this modifier is stateless. Something like Modifier.clickable, on the other hand, would cause problems as it's statefull.
Why do you get same result for A1 and A2?
Because in A2, Canvas ends up with this modifier:
Modifier.fillMaxWidth().weight(0.4f).fillMaxSize().background()
weight has no effect inside Box and fillMaxWidth is included in fillMaxSize, so it's the same as A1 with just fillMaxSize.
Why doesn't Canvas in A1 fill full screen?
Modifier fillMaxSize respects the incoming measurement constraints as stated in its documentation, which means that it won't grow bigger than its parent Box. Modifier requiredSize on the other hand ignores incoming constraints, so child can outgrow its parent.
In addition to fillMaxWidth from parentModifier, you also used fillMaxSize on your ScreenHome_Info, which means it will try to fill max height as well. The only height constraints on ScreenHome_Watch and ScreenHome_Table are the weights. As per the weight documentation:
Size the element's height proportional to its weight relative to other weighted sibling elements in the Column. The parent will divide the vertical space remaining after measuring unweighted child elements and distribute it according to this weight.
This means that ScreenHome_Info as the only unweighted child is measured first, takes all the space and nothing is left for the other two, so they are not visible.
There is no size modifier on the Canvas in C1. In C2, you reuse the parent modifier fillMaxWidth().weight(0.4f), but again, weight has no effect inside of a Box, so in C2, Canvas only has width and no height. And when you don't specify size for Canvas, it's simply 0, as per the documentation:
You MUST specify size with modifier, whether with exact sizes via Modifier.size modifier, or relative to parent [...]
This is also the reason why Canvas, unlike most (all?) of the other components, has the modifier parameter mandatory and not empty Modifier by default.
In the case of C1 Canvas does not have height or width.
In the case of C2 the screenshot C1C2 is probably not what you get if you have passed in Modifier.fillMaxSize().weight(0.4f) as in A1 for ScreenHome_Table.
Your code in c2 this works but would not recommend using same parent Modifier for both box and canvas.
Compose Box layout showing white background when applying Clip Option
Box( modifier = Modifier
.size(50.dp)
.clip(CircleShape)
.background(colorResource(id = R.color.black_trans))
)
Is there any way to transparent the background when CircleClip is applied?
Box( modifier = Modifier
.size(50.dp)
.background(colorResource(id = R.color.black_trans))
.clip(CircleShape)
)
https://developer.android.com/jetpack/compose/modifiers
In Jetpack Compose the order of the modifiers matters. So add background color first and then clip.
Box( modifier = Modifier
.size(50.dp)
.background(colorResource(id = R.color.black_trans))
.clip(CircleShape)
)
Modifier Reference: https://developer.android.com/jetpack/compose/modifiers#order-modifier-matters
Box(
modifier = Modifier
.size(100.dp)
.clip(RoundedCornerShape(25))
.background(Color.Blue)
In Modifier order metters so we have to add background at last.
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*/){
}