0

I tried this but weight is not working here. Is there any way to add weight I want to create a number pad from 1 to 9. For this I want to add 1 lazy row with fillMaxWidth and add 3 items with 1 weight each.

    @Composable
fun NumberBoardRow(num: List<String>) {
    LazyRow(
        modifier = Modifier.fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center
    ) {
        itemsIndexed(num) { num, index ->
            NumberButton(modifier = Modifier.weight(1f), number = index, onClick = {})
        }
    }

}
RF1991
  • 2,037
  • 4
  • 8
  • 17
  • 1
    Can't you use LazyVerticalGrid for achieving your requirement? It would be more convenient – Megh Aug 23 '23 at 10:33
  • Use Lazy vertical grid to do this because it is not possible to give something a weight when it has no maximum size – Ayman Ait Aug 23 '23 at 10:41
  • 1
    It's possible. You can wrap LazyRow with BoxWithConstraints to get LazyRow width via `maxWidth` then using maxWidth/some divider you can assign width proportional as weight does. But what i don't get it is, this was also asked for Column with vertical scroll, point of using scrollable Composable if you wish to assign some weight. Also you can create numpad with LazyVertical grid that displays 9 items . You can also refer this answer https://stackoverflow.com/a/72937753/5457853 – Thracian Aug 23 '23 at 10:47
  • @AymanAit `when it has no maximum size` this is not true, It can have 200.dp width modifier, fillmaxWidh or no modifier even then it's size is limited to parent Constraints. However LazyColumn/Row or a Composable with scroll has size but it passes Constraints(maxWidth=Constraints.Infinity) to content. That's why content can grow – Thracian Aug 23 '23 at 10:49
  • LazyVerticalGrid helped, thank you all – Ravikant sahu Aug 24 '23 at 06:48

2 Answers2

0

How about to give this a try?

@Composable
fun NumberBoardRow(num: List<String>) {
    LazyRow(
        modifier = Modifier.fillMaxWidth().weight(1f),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center
    ) {
        itemsIndexed(num) { num, index ->
            NumberButton(number = index, onClick = {})
        }
    }

}
Sarah
  • 175
  • 4
  • That's the problem weight property will not work in the LazyRow modifier. This will work like that - "@Composable fun NumberBoardRow(num: List, onNumberClick: (num: String) -> Unit) { Row( modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.Center ) { for (i in num) { NumberButton( modifier = Modifier.weight(1f), number = i, onClick = { onNumberClick(it) }) } } }" – Ravikant sahu Aug 24 '23 at 06:14
0

You can do this by two ways:

  1. Without a lazy row or Column like below:

    @Composable fun NumberBoardRow2( num: List, onNumberClick: (num: String) -> Unit ) { Row( modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.Center ) { for (i in num) { NumberButton( modifier = Modifier.weight(1f), number = i, onClick = { onNumberClick(it) }) } }

    }

  2. with LazyVerticalGrid like below :

    @Composable fun NumberBoardRow(num: List, onNumberClick: (num: String) -> Unit) { val list = (1..9).map { it.toString() }.toMutableList() list.addAll(mutableListOf(".", "0", "X"))

     LazyVerticalGrid(
         columns = GridCells.Fixed(3),
         contentPadding = PaddingValues(
             start = 12.dp,
             top = 16.dp,
             end = 12.dp,
             bottom = 16.dp
         ),
         content = {
             itemsIndexed(items = list) { index, item ->
                 NumberButton(
                     modifier = Modifier,
                     number = item,
                     onClick = { onNumberClick(it) })
    
             }
         }
     )
    

    }