Skip to content

Instantly share code, notes, and snippets.

@kozaxinan
Last active August 30, 2023 14:02
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kozaxinan/77dfbc9a0e1adf729e16a984d5af0da5 to your computer and use it in GitHub Desktop.
Save kozaxinan/77dfbc9a0e1adf729e16a984d5af0da5 to your computer and use it in GitHub Desktop.
A Jetpack compose implementation for displaying counter for hidden word becuase of text overflow
package foo
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
@Composable
private fun LocationLabelArea(locations: String) {
Row(
modifier = Modifier
.height(48.dp)
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically
) {
var additional by remember { mutableStateOf(10) }
Box(
modifier = Modifier.weight(1f)
) {
Text(
text = locations,
modifier = Modifier.fillMaxWidth(),
overflow = TextOverflow.Ellipsis,
maxLines = 1,
onTextLayout = { textLayoutResult ->
if (textLayoutResult.hasVisualOverflow) {
val lineEndIndex = textLayoutResult.getLineEnd(
lineIndex = 0,
visibleEnd = true
)
additional = locations
.substring(lineEndIndex)
.count { it == ',' }
}
},
)
}
if (additional != 0) {
CounterBadge(additionalLocationsCount = additional)
}
}
}
@Composable
private fun CounterBadge(additionalLocationsCount: Int) {
Box(
modifier = Modifier
.padding(start = 8.dp)
.size(32.dp)
.clip(RoundedCornerShape(100))
.background(MaterialTheme.colors.onBackground.copy(alpha = 0.3f))
) {
Text(
text = "+$additionalLocationsCount",
overflow = TextOverflow.Visible,
maxLines = 1,
modifier = Modifier
.align(Alignment.Center),
)
}
}
@Preview(showBackground = true)
@Composable
private fun Preview() {
MaterialTheme {
Card(
Modifier.padding(16.dp),
border = BorderStroke(1.dp, MaterialTheme.colors.onSurface),
) {
LocationLabelArea(
locations = "Some location, Some, Some location, Some location, Some location"
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment