How to individually/separately align child composables inside a Row? - android

I'm new in jetpack compose and I'm trying to do a simple thing that I can't achieve it.
That I want to do is in the same row align one component, in this case a surface, at the start and the other one, the column, at the end of the row.
How can get this?
I'm trying this but it doesn't work:
Row(Modifier.padding(top = 24.dp)
.fillMaxWidth()) {
Surface(
modifier = Modifier.size(70.dp),
shape = RectangleShape,
color = MaterialTheme.colors.onSurface.copy(alpha = 0.2f)
) {
// Image goes here
}
Column(modifier = Modifier.size(70.dp)) {
Text("Total",
fontSize = 12.sp,
color = AppGreyDark,
modifier = Modifier.padding(end = 16.dp))
Text("12,99 €",
fontSize = 18.sp,
color = AppBlackDark,
modifier = Modifier.padding(top = 4.dp))
}
}

You can apply in the Row the Arrangement.SpaceBetween.
Row(
modifier = Modifier
.padding(top = 24.dp)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween) {
Surface()
Column()
}

Related

Android Compose scroll up after focus on BasicTextField

I'm using BasicTextField to stylize my text input field. I'm having a problem where when I focus on said text input on the lower part of the screen, keyboard overlaps my input a bit. Is there a way to fix it ? Here's my BasicTextField
Row(
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 68.dp)
.border(width = 1.dp, color = Color.Blue)
.background(color = Color.White),
verticalAlignment = Alignment.Top
) {
BasicTextField(
modifier = Modifier
.offset(y = (8).dp)
.padding(start = 16.dp, top = 20.dp, bottom = 16.dp)
.weight(1f),
textStyle = TextStyle(fontSize = 24.sp, lineHeight = 37.sp),
maxLines = 5,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
value = input,
onValueChange = {
input = it
},
decorationBox = {
Text(
modifier = Modifier.offset(y = (-16).dp),
text = "amount",
fontSize = 12.sp,
color = Color.Gray,
fontWeight = FontWeight.Bold
)
it()
},
)
Row(modifier = Modifier.align(Alignment.CenterVertically)) {
Image(
modifier = Modifier.padding(start = 4.dp, end = 16.dp),
painter = painterResource(id = R.drawable.arrow_down),
contentDescription = null,
colorFilter = ColorFilter.tint(color = Color.Blue)
)
}
}
I tried adding specific height on BasicTextField, removing border and background from Row and adding to BasicTextField, but end result wasn't the text field looking like I want it to. I also need the text field to be able to expand on break line and that's where a lot of times it went bad when trying to adjust it. Maybe I'm missing something.

How to achieve layout with jetpack compose where icon is position absolute on column layout

I have comment box and need to put icon on specific position ( bottom right ). I need to make something like position absolute where my icon button need to be bottom right inside comment box. Here is image what I am trying to achieve. Any help or idea?
You can do it by using Modifier.offset{} and putting your Icon inside a Box with Modifier.align(Alignment.BottomEnd)
#Composable
private fun Test() {
Column(modifier = Modifier.padding(10.dp)) {
Box(
modifier = Modifier
.width(200.dp)
.background(Color.LightGray.copy(alpha = .5f), RoundedCornerShape(8.dp))
.padding(4.dp)
) {
Column(
modifier = Modifier.fillMaxWidth()
) {
Text("Title", fontSize = 20.sp)
Text("Comment")
}
val offsetInPx = with(LocalDensity.current) {
16.dp.roundToPx()
}
Icon(
imageVector = Icons.Default.Settings,
contentDescription = null,
modifier = Modifier
.offset {
IntOffset(-offsetInPx, offsetInPx)
}
.shadow(2.dp, RoundedCornerShape(40))
.background(Color.White)
.padding(horizontal = 10.dp, vertical = 4.dp)
.size(30.dp)
.align(Alignment.BottomEnd),
tint = Color.LightGray
)
}
Spacer(modifier = Modifier.height(4.dp))
Text("Reply", color = Color.Blue)
}
}

Is possible to resize a view inside a fix size Box in compose

That I want to do is have a row that in the left side I can add a custom view and in the right side I need a colum with two text.
My problem comes in the left side. If I don't put a width in the left element (box) it occupies all the row and if I put a specific size of the element (box) this show the custom view looks flattened.
How can I add my customview align at the left part of the Row keeping the correct size of my custom view?
Row(
Modifier
.padding(top = 24.dp)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween) {
Box(modifier = Modifier
.size(width= 115.dp, height= 40.dp)
.align(Alignment.Bottom)){
Box(modifier = Modifier
.align(Alignment.BottomEnd)
.wrapContentSize()){
AndroidView(
factory = { context ->
MyView(context).apply {
seteViewListener(listener)
}
},
)
}
}
Column(modifier = Modifier.align(Alignment.Bottom)) {
Text("Total",
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.End,
fontSize = 12.sp,
lineHeight = 16.sp,
color = colorResource(id = R.color.app_grey_dark))
Text(text = currentItem.totalPrice,
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.End,
fontSize = 18.sp,
fontWeight = FontWeight.W500,
lineHeight = 24.sp,
color = colorResource(id = R.color.app_black_dark))
}
}

How can I align different items inside LazyColumn in jetpackCompose

I am building a chat app with firebase and I need to align the chat bubbles in the end when I write the message and in the start when I receive, like in whatsapp. If I use the horizontalArrangement in the lazyColumn it affects all the items. I tried using the modifier.align in the chat bubbles but nothing happens. How can I do this?
below is my lazyColumn
LazyColumn(
modifier = Modifier.fillMaxWidth(),
) {
if (list != null && list.isNotEmpty()) {
items(items = list) {
if (it.user1id == args.userId) {
ChatCard(
message = it,
color = Color.Magenta,
modifier = Modifier
.align(Alignment.CenterHorizontally)
.padding(
start = 32.dp,
end = 4.dp,
top = 4.dp
)
)
} else {
ChatCard(
message = it,
color = Color.White,
Modifier.padding(
start = 4.dp,
end = 32.dp,
top = 4.dp
)
)
}
}
}
}
#Composable
fun ChatCard(message: Message, color: Color, modifier: Modifier = Modifier){
Card(
modifier = modifier,
backgroundColor = color,
shape = RoundedCornerShape(10.dp)
) {
Row(
modifier = Modifier.padding(4.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
modifier = Modifier
.padding(4.dp)
.widthIn(0.dp, 280.dp),
text = message.message
)
Text(
modifier = Modifier.padding(4.dp),
text = message.time,
style = TextStyle(
fontSize = 12.sp,
color = Color.LightGray
)
)
}
}
}
You can add a Row for each item applying a different horizontalArrangement removing the Modifier.align in your Card.
Something like:
items(items = itemsList) {
Row(Modifier.fillMaxWidth(),
horizontalArrangement = if (it == ....)
Arrangement.Start else
Arrangement.End) {
if (it == ....) {
ChatCard(
color = Color.Magenta,
modifier = Modifier
.padding(
start = 32.dp,
end = 4.dp,
top = 4.dp
)
)
} else {
ChatCard(
color = Color.White,
Modifier.padding(
start = 4.dp,
end = 32.dp,
top = 4.dp
)
)
}
}

How to get Image with text on top/infront? (Google Classroom Home Image Format)

I want to make an Image with Text on top, just like Google CLassroom. But first I want to test Image and then Text. Instead, I got the image overlapping the text. Image Overlapping text
Then I move the Image code after the text. How to get simple G classroom format
Text then Image
#Composable
fun ClassImage(
// icon: VectorAsset,
// label: String,
// modifier: Modifier = Modifier
) {
val imageAlpha = 1f
Surface(
modifier = Modifier
.padding(start = 8.dp, top = 8.dp, end = 8.dp)
.fillMaxWidth(),
color = colors.primary.copy(alpha = 0.12f)
) {
TextButton(
onClick = {},
modifier = Modifier.fillMaxWidth()
) {
Row(
horizontalArrangement = Arrangement.Start,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth())
{
Image(
imageResource(id = R.drawable.class1),
alpha = imageAlpha
)
Column {
Text("Alfred Sisley", fontWeight = FontWeight.Bold)
ProvideEmphasis(emphasis = EmphasisAmbient.current.medium) {
Text("3 minutes ago", style = MaterialTheme.typography.body2)
}
}
}
}
}
}
To put Text on top of the Image you can use Box, which is similar to old FrameLayout.
I'm not sure want you wanted to achieve, but if smth like this:
Then you can do it this way:
Surface(
shape = RoundedCornerShape(8.dp),
modifier = Modifier
.preferredHeight(128.dp)
.clickable(onClick = {})
) {
Box {
Image(
vectorResource(id = R.drawable.ic_launcher_background),
alpha = imageAlpha,
contentScale = ContentScale.Crop,
modifier = Modifier.fillMaxSize()
)
Column(modifier = Modifier.padding(16.dp)) {
Text(
"Alfred Sisley",
fontWeight = FontWeight.Bold,
style = MaterialTheme.typography.h6)
ProvideEmphasis(emphasis = EmphasisAmbient.current.medium) {
Text("3 minutes ago", style = MaterialTheme.typography.body2)
}
Spacer(modifier = Modifier.weight(1f))
Text(text = "Footer", style = MaterialTheme.typography.body1)
}
}
}
With 1.0.0-beta02 you can use a Box as parent container.
Something like:
Box(modifier = Modifier.height(IntrinsicSize.Max))
{
Image(
painterResource(id = R.drawable.xx),
"contentDescription",
alpha = 0.8f,
modifier = Modifier.requiredHeight(100.dp)
)
Column(
modifier = Modifier.fillMaxHeight(),
verticalArrangement = Arrangement.Bottom) {
Text("Alfred Sisley",
fontWeight = FontWeight.Bold)
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
Text("3 minutes ago", style = MaterialTheme.typography.body2)
}
}
}

Categories

Resources