Search code examples
androidkotlintextandroid-jetpack-compose

Is there a way to set the height of a text regarding there content?


I have a composable dialog with different text(views). One of the textviews is inside a scrollable column cause the content of the text can be long. In that case I will set the height of the column to "fillMaxHeight(0.5f)". But when the text is short enough I will "wrapContentHeight()". Problem is that I dunno know how big the screen can be and how much space the text can use to be displayed. So I thought there must be something like use so much space you need but only to max (left screenheight) or so. So if i use fillmaxheight it will also claim the space even if it don't need the space. How can I achieve my goal?

here the code of the dialog:

@Composable
private fun DetailDialog() {
    val context = LocalContext.current
    val game = mSelectedGame

    Dialog(
        icon = painterResource(R.mipmap.ic_dice),
        title = stringResource(R.string.title_details),
        actionIcon = painterResource(R.mipmap.ic_close),
        actionsDescription = stringResource(R.string.close),
        dialogProperties = DialogProperties(dismissOnClickOutside = true),
        onActionClick = {
            mGameClicked.value = false
            mSelectedGame = UnknownGame.copy()
        },
        onDismissRequest = {
            mGameClicked.value = false
            mSelectedGame = UnknownGame.copy()
        }) {
        Column(
            modifier = Modifier.padding(horizontal = 10.dp)
        ) {
            Spacer(Modifier.height(5.dp))
            Entry(painterResource(R.mipmap.ic_identification_documents), stringResource(R.string.name), game.name)
            Spacer(Modifier.height(5.dp))
            Entry(painterResource(R.mipmap.ic_timespan), stringResource(R.string.time_span), "${game.minTime} - ${game.maxTime} min")
            Spacer(Modifier.height(5.dp))
            Entry(painterResource(R.mipmap.ic_user_groups), stringResource(R.string.persons), "${game.minPerson} - ${game.maxPerson}")
            Spacer(Modifier.height(5.dp))
            Entry(painterResource(R.mipmap.ic_dice), stringResource(R.string.genres), game.genres.value.stream().map { it.name }.toArray().joinToString())
            Spacer(Modifier.height(5.dp))
            Entry(painterResource(R.mipmap.ic_information), stringResource(R.string.description), game.description, maxLines = false)
            Spacer(Modifier.height(5.dp))
            Row(
                Modifier
                    .fillMaxWidth()
                    .align(CenterHorizontally)
            ) {
                Spacer(modifier = Modifier.weight(1f))
                OutlinedTextButton(painterResource(R.mipmap.ic_edit), stringResource(R.string.edit)) { startForResult.launch(Intent(context, EditGamesActivity::class.java).apply { putExtra(EXTRA_GAME_ID, game.id) }) }
                OutlinedTextButton(painterResource(R.mipmap.ic_waste), stringResource(R.string.delete)) { }
                Spacer(modifier = Modifier.weight(1f))
            }
        }
    }
}

@Composable
private fun Entry(icon: Painter, contentDescription: String, text: String, size: TextUnit = 16.sp, maxLines: Boolean = true) {
    Row(Modifier.fillMaxWidth()) {
        Image(
            modifier = Modifier
                .verticalGradient()
                .align(CenterVertically)
                .padding(5.dp),
            painter = icon,
            contentDescription = contentDescription
        )
        if (!maxLines) {
            Column(
                modifier = Modifier
                    .align(CenterVertically)
                    .fillMaxHeight(0.5f)
                    .padding(start = 5.dp)
                    .verticalScrollWithScrollbar(
                        rememberScrollState(),
                        scrollbarConfig = ScrollBarConfig(
                            padding = PaddingValues(4.dp, 4.dp, 4.dp, 4.dp),
                            showAlways = true
                        )
                    )
            ) {
                Text(
                    modifier = Modifier.padding(end = 10.dp),
                    text = text,
                    fontSize = size,
                    color = MaterialTheme.colorScheme.onBackground
                )
            }
        } else {
            Text(
                modifier = Modifier
                    .align(CenterVertically)
                    .padding(start = 5.dp),
                text = text,
                fontSize = size,
                color = MaterialTheme.colorScheme.onBackground
            )
        }
    }
}

Solution

  • You Can use Modifier.heightIn and set max

    Modifier.heightIn(max = (LocalConfiguration.current.screenHeightDp/2).dp)