Skip to content

Instantly share code, notes, and snippets.

@Kiolk
Last active December 19, 2024 21:42
Show Gist options
  • Save Kiolk/240c0ab6f0b6aeec77ffe8273f9ab2e1 to your computer and use it in GitHub Desktop.
Save Kiolk/240c0ab6f0b6aeec77ffe8273f9ab2e1 to your computer and use it in GitHub Desktop.
How to Handle the "More" Feature on Android?
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
@Composable
fun ShowMoreComposable() {
Column(
modifier = Modifier
.fillMaxSize()
) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(140.dp)
)
ContentBlock()
Box(
modifier = Modifier
.fillMaxWidth()
.weight(1f)
)
}
}
@Composable
fun ContentBlock() {
val isShowMore = remember { mutableStateOf(false) }
Box(
modifier = Modifier
.fillMaxWidth()
.height(160.dp)
.background(color = Color.Yellow, shape = RoundedCornerShape(8.dp))
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(8.dp),
verticalArrangement = Arrangement.Bottom
) {
Text(
text = mockedText,
modifier = Modifier
.weight(0.85f),
overflow = TextOverflow.Ellipsis,
onTextLayout = { result ->
isShowMore.value = result.hasVisualOverflow
}
)
if (isShowMore.value) {
Text(
text = "More",
modifier = Modifier
.weight(0.15f)
.align(Alignment.End)
)
}
}
}
}
val mockedText =
"On this website we use cookies and similar functions to process end device information and personal data (e.g. such as IP-addresses or browser information). The processing is used for purposes such as to integrate content, external services and elements from third parties, statistical analysis/measurement, personalized advertising and the integration of social media. Depending on the"
@Preview(showBackground = true)
@Composable
fun ShowMoreComposablePreview() {
ShowMoreComposable()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment