0

I'm writing an interface in Kotlin using Jetpack Compose. I'm just learning and there is a problem with button layout.

I want to make "Check" and "Delete" buttons to be located right after the input field. Take a look at the screenshots, how it looks like now and how I am trying to do it.

Code:


Row(
    Modifier.fillMaxWidth(),
    verticalAlignment = Alignment.CenterVertically,
    horizontalArrangement = Arrangement.SpaceBetween
) {
    Text(text = "Players", style = Typography.titleLarge)

    IconButton(onClick = { isNewPlayer = true }) {
        Icon(imageVector = Icons.Default.Add, contentDescription = "")
    }
}

if (isNewPlayer) {

    EditTextFieldSmall(
        label = "New Player",
        value = newPlayer.toStringWithEmpty(),
        onValueChange = { newPlayer = it.toIntWithEmpty() }
    )

    IconButton(
        onClick = {
            val newList = club.player.toMutableList()

            newList.add(newPlayer)

            club = club.copy(player = newList)

            newPlayer = 0

            isNewPlayer = false
        }
    ) {
        Icon(
            imageVector = Icons.Default.Check,
            contentDescription = "",
            tint = Color.Green,
            modifier = Modifier.size(16.dp)
        )
    }

    IconButton(
        onClick = {
            newPlayer = 0

            isNewPlayer = false
        }
    ) {
        Icon(
            imageVector = Icons.Default.Delete,
            contentDescription = "",
            tint = Color.Red,
            modifier = Modifier.size(16.dp)
        )
    }
}

for (i in club.player.indices) {

    EditTextFieldSmall(
        label = "Player",
        value = club.player[i].toStringWithEmpty(),
        onValueChange = {
            val newList = club.player.toMutableList()

            newList[i] = it.toIntWithEmpty()

            club = club.copy(player = newList)
        }
    )

    IconButton(
        onClick = {
            val newList = club.player.toMutableList()

            newList.removeAt(i)

            club = club.copy(player = newList)
        }
    ) {
        Icon(
            imageVector = Icons.Default.Delete,
            contentDescription = "",
            tint = Color.Red,
            modifier = Modifier.size(16.dp)
        )
    }
}

And:


@Composable
fun EditTextFieldSmall(label: String, value: String, onValueChange: (String) -> Unit) {

    Row(Modifier.fillMaxWidth().padding(top = 4.dp)) {
        Text(text = label, color = Color.White)

        Spacer(modifier = Modifier.width(8.dp))

        BasicTextField(
            modifier =
                Modifier.width(130.dp)
                    .height(24.dp)
                    .background(EditTextFieldBackground, shapes.extraSmall),
            value = value,
            onValueChange = { newValue ->
                if (newValue.length <= 6) {

                    onValueChange(newValue)
                }
            },
            textStyle =
                LocalTextStyle.current.copy(
                    color = EditTextFieldTextColor,
                    textAlign = TextAlign.Center
                ),
            keyboardOptions =
                KeyboardOptions(keyboardType = KeyboardType.Text, imeAction = ImeAction.Done)
        )
    }
}

The screenshot above is what it looks like now:

// Image

Bottom screen - how I want it to look

Now it looks like this That's what I want to do

Tried adding Row and Column before BasicTextField

1 Answer 1

1

Please try to wrap all occurences of these three Composables with an additional Row like this:

Row {
    EditTextFieldSmall(
        //...
    )

    IconButton(
        //...
    ) {
        //...
    }

    IconButton(
        //...
    ) {
        //...
    }
}

Also, remove the fillMaxWidth() Modifier from the Row inside of the EditTextFieldSmall Composable:

@Composable
fun EditTextFieldSmall(label: String, value: String, onValueChange: (String) -> Unit) {

    Row(Modifier.padding(top = 4.dp)) {
        //...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.